websocket-sharp/Example/Program.cs

109 lines
3.7 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
{
2014-10-01 15:42:40 +08:00
/* Create a new instance of the WebSocket class.
*
2014-10-05 12:32:58 +08:00
* The WebSocket class inherits the System.IDisposable interface, so you can use
* the using statement. And the WebSocket connection has been closed with close
* status 1001 (going away) when the control leaves the using block.
2014-10-01 15:42:40 +08:00
*
2014-10-05 12:32:58 +08:00
* If you would like to connect to the server with the secure connection, you should
* create the instance with the wss scheme WebSocket URL.
2014-10-01 15:42:40 +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"))
//using (var ws = new WebSocket ("ws://localhost:4649/Echo?name=nobita"))
2014-10-01 15:42:40 +08:00
//using (var ws = new WebSocket ("wss://localhost:4649/Echo"))
2014-03-11 15:41:43 +08:00
//using (var ws = new WebSocket ("ws://localhost:4649/Chat"))
//using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
2014-10-01 15:42:40 +08:00
//using (var ws = new WebSocket ("wss://localhost:4649/Chat"))
2012-07-31 09:36:52 +08:00
{
2014-10-01 15:42:40 +08:00
// To 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) =>
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"
});
2014-10-01 15:42:40 +08:00
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.
ws.WaitTime = TimeSpan.FromSeconds (10);
2014-03-11 15:41:43 +08:00
#endif
2014-10-01 15:42:40 +08:00
// To negotiate the Per-message Compression extension.
//ws.Compression = CompressionMethod.Deflate;
2014-03-11 15:41:43 +08:00
2014-10-05 12:32:58 +08:00
/* To validate the server certificate.
ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
ws.Log.Debug (String.Format ("\n{0}\n{1}", certificate.Issuer, certificate.Subject));
return true; // If the server certificate is valid.
};
*/
2014-03-11 15:41:43 +08:00
2014-10-01 15:42:40 +08:00
// To set 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
2014-10-01 15:42:40 +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");
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
}
}
}
}
}