websocket-sharp/Example/Program.cs

129 lines
4.3 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
{
2014-10-01 15:42:40 +08:00
public static void Main (string[] args)
2012-07-31 09:36:52 +08:00
{
2015-11-15 14:36:01 +08:00
// Create a new instance of the WebSocket class.
//
// The WebSocket class inherits the System.IDisposable interface, so you can
// use the using statement. And the WebSocket connection will be closed with
// close status 1001 (going away) when the control leaves the using block.
//
2016-08-04 16:46:04 +08:00
// If you would like to connect to the server with the secure connection,
2016-07-24 16:04:25 +08:00
// you should create a new instance with a wss scheme WebSocket URL.
2015-11-15 14:36:01 +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-10-01 15:42:40 +08:00
//using (var ws = new WebSocket ("wss://echo.websocket.org"))
2014-03-11 15:41:43 +08:00
//using (var ws = new WebSocket ("ws://localhost:4649/Echo"))
2016-07-24 16:04:25 +08:00
//using (var ws = new WebSocket ("wss://localhost:5963/Echo"))
2014-03-11 15:41:43 +08:00
//using (var ws = new WebSocket ("ws://localhost:4649/Echo?name=nobita"))
2016-07-24 16:04:25 +08:00
//using (var ws = new WebSocket ("wss://localhost:5963/Echo?name=nobita"))
2014-03-11 15:41:43 +08:00
//using (var ws = new WebSocket ("ws://localhost:4649/Chat"))
2016-07-24 16:04:25 +08:00
//using (var ws = new WebSocket ("wss://localhost:5963/Chat"))
2014-03-11 15:41:43 +08:00
//using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
2016-07-24 16:04:25 +08:00
//using (var ws = new WebSocket ("wss://localhost:5963/Chat?name=nobita"))
2012-07-31 09:36:52 +08:00
{
2015-11-15 14:36:01 +08:00
// Set the 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) =>
2016-07-24 16:04:25 +08:00
nf.Notify (
new NotificationMessage {
Summary = "WebSocket Message",
Body = !e.IsPing ? e.Data : "Received a ping.",
Icon = "notification-message-im"
}
);
2012-07-31 09:36:52 +08:00
ws.OnError += (sender, e) =>
2016-07-24 16:04:25 +08:00
nf.Notify (
new NotificationMessage {
Summary = "WebSocket Error",
Body = e.Message,
Icon = "notification-message-im"
}
);
2012-07-31 09:36:52 +08:00
ws.OnClose += (sender, e) =>
2016-07-24 16:04:25 +08:00
nf.Notify (
new NotificationMessage {
Summary = String.Format ("WebSocket Close ({0})", e.Code),
Body = e.Reason,
Icon = "notification-message-im"
}
);
2014-03-11 15:41:43 +08:00
#if DEBUG
2014-10-01 15:42:40 +08:00
// To change the logging level.
ws.Log.Level = LogLevel.Trace;
2014-10-01 15:42:40 +08:00
// To change the wait time for the response to the Ping or Close.
2016-07-24 16:04:25 +08:00
//ws.WaitTime = TimeSpan.FromSeconds (10);
2015-07-26 16:25:08 +08:00
2015-11-15 14:36:01 +08:00
// To emit a WebSocket.OnMessage event when receives a ping.
2016-07-24 16:04:25 +08:00
//ws.EmitOnPing = true;
2014-03-11 15:41:43 +08:00
#endif
2014-11-11 09:44:37 +08:00
// To enable the Per-message Compression extension.
//ws.Compression = CompressionMethod.Deflate;
2014-03-11 15:41:43 +08:00
2016-08-05 14:15:35 +08:00
// To validate the server certificate.
/*
ws.SslConfiguration.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => {
ws.Log.Debug (
String.Format (
"Certificate:\n- Issuer: {0}\n- Subject: {1}",
certificate.Issuer,
2016-07-24 16:04:25 +08:00
certificate.Subject
)
);
return true; // If the server certificate is valid.
};
2014-10-05 12:32:58 +08:00
*/
2014-03-11 15:41:43 +08:00
2015-11-15 14:36:01 +08:00
// To send the credentials for the 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
2014-10-01 15:42:40 +08:00
// To send the Origin header.
2014-03-28 16:00:16 +08:00
//ws.Origin = "http://localhost:4649";
2012-07-31 09:36:52 +08:00
2016-07-24 16:04:25 +08:00
// To send the 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-10-01 15:42:40 +08:00
// To connect through the HTTP Proxy server.
2014-08-07 18:43:14 +08:00
//ws.SetProxy ("http://localhost:3128", "nobita", "password");
2016-08-04 16:53:09 +08:00
// To enable the redirection.
//ws.EnableRedirection = true;
2014-10-01 15:42:40 +08:00
// Connect to the server.
2014-03-11 15:41:43 +08:00
ws.Connect ();
2014-10-01 15:42:40 +08:00
// Connect to the server asynchronously.
2014-03-11 15:41:43 +08:00
//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-10-01 15:42:40 +08:00
// Send a text message.
2014-03-11 15:41:43 +08:00
ws.Send (msg);
2012-07-31 09:36:52 +08:00
}
}
}
}
}