Modified a few for Example

This commit is contained in:
sta 2014-10-01 16:42:40 +09:00
parent 6ff6b0485f
commit e80633b73b
2 changed files with 38 additions and 28 deletions

View File

@ -35,9 +35,10 @@ namespace Example
Console.WriteLine (msg); Console.WriteLine (msg);
#endif #endif
} }
else else {
Thread.Sleep (500); Thread.Sleep (500);
} }
}
_waitHandle.Set (); _waitHandle.Set ();
}); });
@ -45,20 +46,18 @@ namespace Example
public int Count { public int Count {
get { get {
lock (_sync) { lock (_sync)
return _queue.Count; return _queue.Count;
} }
} }
}
private NotificationMessage dequeue () private NotificationMessage dequeue ()
{ {
lock (_sync) { lock (_sync)
return _queue.Count > 0 return _queue.Count > 0
? _queue.Dequeue () ? _queue.Dequeue ()
: null; : null;
} }
}
public void Close () public void Close ()
{ {
@ -69,11 +68,10 @@ namespace Example
public void Notify (NotificationMessage message) public void Notify (NotificationMessage message)
{ {
lock (_sync) { lock (_sync)
if (_enabled) if (_enabled)
_queue.Enqueue (message); _queue.Enqueue (message);
} }
}
void IDisposable.Dispose () void IDisposable.Dispose ()
{ {

View File

@ -7,19 +7,28 @@ namespace Example
{ {
public class Program public class Program
{ {
public static void Main (string [] args) public static void Main (string[] args)
{ {
/* Create a new instance of the WebSocket class.
*
* The WebSocket class inherits the System.IDisposable interface, so you can use the using
* statement. The WebSocket connection has been closed with close status 1001 (going away)
* when the control leaves the using block.
*
* If you would like to connect to the server with the secure connection, you should create
* the instance with the wss scheme WebSocket URL.
*/
using (var nf = new Notifier ()) using (var nf = new Notifier ())
using (var ws = new WebSocket ("ws://echo.websocket.org")) using (var ws = new WebSocket ("ws://echo.websocket.org"))
//using (var ws = new WebSocket ("wss://echo.websocket.org")) // For Secure Connection //using (var ws = new WebSocket ("wss://echo.websocket.org"))
//using (var ws = new WebSocket ("ws://localhost:4649/Echo")) //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/Echo?name=nobita"))
//using (var ws = new WebSocket ("wss://localhost:4649/Echo"))
//using (var ws = new WebSocket ("ws://localhost:4649/Chat")) //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")) //using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
//using (var ws = new WebSocket ("wss://localhost:4649/Chat"))
{ {
/* Setting WebSocket events */ // To set the WebSocket events.
ws.OnOpen += (sender, e) => ws.Send ("Hi, there!"); ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");
ws.OnMessage += (sender, e) => ws.OnMessage += (sender, e) =>
@ -47,35 +56,38 @@ namespace Example
}); });
#if DEBUG #if DEBUG
// Changing the logging level // To change the logging level.
ws.Log.Level = LogLevel.Trace; ws.Log.Level = LogLevel.Trace;
// To change the wait time for the response to the Ping or Close.
ws.WaitTime = TimeSpan.FromSeconds (10);
#endif #endif
// Setting Per-message Compression // To negotiate the Per-message Compression extension.
//ws.Compression = CompressionMethod.Deflate; //ws.Compression = CompressionMethod.Deflate;
/* For Secure Connection // To validate the server certificate.
ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { //ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
ws.Log.Debug (String.Format ("\n{0}\n{1}", certificate.Issuer, certificate.Subject)); // ws.Log.Debug (String.Format ("\n{0}\n{1}", certificate.Issuer, certificate.Subject));
return true; // If the server cert is valid // return true; // If the server certificate is valid.
}; //};
*/
// For HTTP Authentication (Basic/Digest) // To set the credentials for the HTTP Authentication (Basic/Digest).
//ws.SetCredentials ("nobita", "password", false); //ws.SetCredentials ("nobita", "password", false);
// Setting Origin header // To send the Origin header.
//ws.Origin = "http://echo.websocket.org";
//ws.Origin = "http://localhost:4649"; //ws.Origin = "http://localhost:4649";
// Setting Cookies // To send the Cookies.
//ws.SetCookie (new Cookie ("name", "nobita")); //ws.SetCookie (new Cookie ("name", "nobita"));
//ws.SetCookie (new Cookie ("roles", "\"idiot, gunfighter\"")); //ws.SetCookie (new Cookie ("roles", "\"idiot, gunfighter\""));
// Setting Proxy // To connect through the HTTP Proxy server.
//ws.SetProxy ("http://localhost:3128", "nobita", "password"); //ws.SetProxy ("http://localhost:3128", "nobita", "password");
// Connecting to the server // Connect to the server.
ws.Connect (); ws.Connect ();
// Connect to the server asynchronously.
//ws.ConnectAsync (); //ws.ConnectAsync ();
Console.WriteLine ("\nType 'exit' to exit.\n"); Console.WriteLine ("\nType 'exit' to exit.\n");
@ -86,7 +98,7 @@ namespace Example
if (msg == "exit") if (msg == "exit")
break; break;
// Sending a text message // Send a text message.
ws.Send (msg); ws.Send (msg);
} }
} }