websocket-sharp/Example/Program.cs

96 lines
3.0 KiB
C#
Raw Normal View History

2012-07-31 09:36:52 +08:00
using System;
using System.Threading;
using WebSocketSharp;
2013-04-06 15:21:41 +08:00
using WebSocketSharp.Net;
2012-07-31 09:36:52 +08:00
2014-03-11 15:41:43 +08:00
namespace Example
{
public class Program
{
public static void Main (string [] args)
2012-07-31 09:36:52 +08:00
{
2014-03-11 15:41:43 +08:00
using (var nf = new Notifier ())
using (var ws = new WebSocket ("ws://echo.websocket.org"))
2014-03-16 03:25:19 +08:00
//using (var ws = new WebSocket ("wss://echo.websocket.org")) // For Secure Connection
2014-03-11 15:41:43 +08:00
//using (var ws = new WebSocket ("ws://localhost:4649/Echo"))
//using (var ws = new WebSocket ("wss://localhost:4649/Echo"))
//using (var ws = new WebSocket ("ws://localhost:4649/Echo?name=nobita"))
//using (var ws = new WebSocket ("ws://localhost:4649/Chat"))
//using (var ws = new WebSocket ("wss://localhost:4649/Chat"))
//using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
2012-07-31 09:36:52 +08:00
{
2014-03-16 03:25:19 +08:00
/* Setting WebSocket events */
2014-03-11 15:41:43 +08:00
ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");
2012-07-31 09:36:52 +08:00
ws.OnMessage += (sender, e) =>
2014-03-11 15:41:43 +08:00
nf.Notify (
2014-03-13 15:08:28 +08:00
new NotificationMessage {
2014-03-11 15:41:43 +08:00
Summary = "WebSocket Message",
Body = e.Data,
Icon = "notification-message-im"
});
2012-07-31 09:36:52 +08:00
ws.OnError += (sender, e) =>
2014-03-11 15:41:43 +08:00
nf.Notify (
2014-03-13 15:08:28 +08:00
new NotificationMessage {
2014-03-11 15:41:43 +08:00
Summary = "WebSocket Error",
Body = e.Message,
Icon = "notification-message-im"
});
2012-07-31 09:36:52 +08:00
ws.OnClose += (sender, e) =>
2014-03-11 15:41:43 +08:00
nf.Notify (
2014-03-13 15:08:28 +08:00
new NotificationMessage {
2014-03-11 15:41:43 +08:00
Summary = String.Format ("WebSocket Close ({0})", e.Code),
Body = e.Reason,
Icon = "notification-message-im"
});
#if DEBUG
2014-03-30 14:52:08 +08:00
// Changing the logging level
ws.Log.Level = LogLevel.Trace;
2014-03-11 15:41:43 +08:00
#endif
2014-03-16 03:25:19 +08:00
// Setting Per-message Compression
//ws.Compression = CompressionMethod.Deflate;
2014-03-11 15:41:43 +08:00
2014-03-16 03:25:19 +08:00
/* For Secure Connection
2014-03-11 15:41:43 +08:00
ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
ws.Log.Debug (String.Format ("\n{0}\n{1}", certificate.Issuer, certificate.Subject));
return true; // If the server cert is valid
};
*/
2014-03-16 03:25:19 +08:00
// For HTTP Authentication (Basic/Digest)
2014-03-17 15:54:13 +08:00
//ws.SetCredentials ("nobita", "password", false);
2014-03-11 15:41:43 +08:00
// For HTTP Proxy
//ws.SetHttpProxy ("http://localhost:3128", "nobita", "password");
2014-03-17 15:54:13 +08:00
// Setting Origin header
2013-07-19 16:29:58 +08:00
//ws.Origin = "http://echo.websocket.org";
2014-03-28 16:00:16 +08:00
//ws.Origin = "http://localhost:4649";
2012-07-31 09:36:52 +08:00
2014-03-16 03:25:19 +08:00
// Setting Cookies
2014-03-28 16:00:16 +08:00
//ws.SetCookie (new Cookie ("name", "nobita"));
2014-03-30 14:52:08 +08:00
//ws.SetCookie (new Cookie ("roles", "\"idiot, gunfighter\""));
2012-07-31 09:36:52 +08:00
2014-03-28 16:00:16 +08:00
// Connecting to the server
2014-03-11 15:41:43 +08:00
ws.Connect ();
//ws.ConnectAsync ();
2012-07-31 09:36:52 +08:00
2014-03-30 14:52:08 +08:00
Console.WriteLine ("\nType 'exit' to exit.\n");
2014-03-11 15:41:43 +08:00
while (true) {
2014-03-16 03:25:19 +08:00
Thread.Sleep (1000);
2014-03-11 15:41:43 +08:00
Console.Write ("> ");
var msg = Console.ReadLine ();
2014-03-13 15:08:28 +08:00
if (msg == "exit")
2012-07-31 09:36:52 +08:00
break;
2014-03-28 16:00:16 +08:00
// Sending a text message
2014-03-11 15:41:43 +08:00
ws.Send (msg);
2012-07-31 09:36:52 +08:00
}
}
}
}
}