diff --git a/Example/Example.csproj b/Example/Example.csproj
index 8cf6a8c1..38c5b420 100644
--- a/Example/Example.csproj
+++ b/Example/Example.csproj
@@ -10,7 +10,6 @@
Example
example
v3.5
- .
true
@@ -35,7 +34,7 @@
full
false
bin\Debug_Ubuntu
- DEBUG,NOTIFY
+ DEBUG,UBUNTU
prompt
4
true
@@ -44,7 +43,7 @@
none
false
bin\Release_Ubuntu
- NOTIFY
+ UBUNTU
prompt
4
true
@@ -66,5 +65,7 @@
+
+
\ No newline at end of file
diff --git a/Example/NotificationMessage.cs b/Example/NotificationMessage.cs
new file mode 100644
index 00000000..631dc787
--- /dev/null
+++ b/Example/NotificationMessage.cs
@@ -0,0 +1,28 @@
+using System;
+
+namespace Example
+{
+ internal class NotificationMessage
+ {
+ public NotificationMessage ()
+ {
+ }
+
+ public string Body {
+ get; set;
+ }
+
+ public string Icon {
+ get; set;
+ }
+
+ public string Summary {
+ get; set;
+ }
+
+ public override string ToString ()
+ {
+ return String.Format ("[{0}: {1}]", Summary, Body);
+ }
+ }
+}
diff --git a/Example/Notifier.cs b/Example/Notifier.cs
new file mode 100644
index 00000000..dff2bb81
--- /dev/null
+++ b/Example/Notifier.cs
@@ -0,0 +1,77 @@
+#if UBUNTU
+using Notifications;
+#endif
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Threading;
+
+namespace Example
+{
+ internal class Notifier : IDisposable
+ {
+ private bool _enabled;
+ private Queue _queue;
+ private ManualResetEvent _waitHandle;
+
+ public Notifier ()
+ {
+ _enabled = true;
+ _queue = new Queue ();
+ _waitHandle = new ManualResetEvent (false);
+
+ ThreadPool.QueueUserWorkItem (
+ state => {
+ while (_enabled || Count > 0) {
+ Thread.Sleep (500);
+ if (Count > 0) {
+ var msg = dequeue ();
+#if UBUNTU
+ var nf = new Notification (msg.Summary, msg.Body, msg.Icon);
+ nf.AddHint ("append", "allowed");
+ nf.Show ();
+#else
+ Console.WriteLine (msg);
+#endif
+ }
+ }
+
+ _waitHandle.Set ();
+ });
+ }
+
+ public int Count {
+ get {
+ lock (((ICollection) _queue).SyncRoot) {
+ return _queue.Count;
+ }
+ }
+ }
+
+ private NotificationMessage dequeue ()
+ {
+ lock (((ICollection) _queue).SyncRoot) {
+ return _queue.Dequeue ();
+ }
+ }
+
+ public void Close ()
+ {
+ _enabled = false;
+ _waitHandle.WaitOne ();
+ _waitHandle.Close ();
+ }
+
+ public void Notify (NotificationMessage message)
+ {
+ lock (((ICollection) _queue).SyncRoot) {
+ _queue.Enqueue (message);
+ }
+ }
+
+ void IDisposable.Dispose ()
+ {
+ Close ();
+ }
+ }
+}
diff --git a/Example/Program.cs b/Example/Program.cs
index c6be15a8..9051867e 100644
--- a/Example/Program.cs
+++ b/Example/Program.cs
@@ -1,154 +1,92 @@
-#if NOTIFY
-using Notifications;
-#endif
using System;
-using System.Collections;
-using System.Linq;
using System.Threading;
using WebSocketSharp;
using WebSocketSharp.Net;
-namespace Example {
-
- public struct NfMessage {
-
- public string Summary;
- public string Body;
- public string Icon;
- }
-
- public class ThreadState {
-
- public bool Enabled { get; set; }
- public AutoResetEvent Notification { get; private set; }
-
- public ThreadState()
+namespace Example
+{
+ public class Program
+ {
+ public static void Main (string [] args)
{
- Enabled = true;
- Notification = new AutoResetEvent(false);
- }
- }
-
- public class Program {
-
- private static Queue _msgQ = Queue.Synchronized(new Queue());
-
- private static void enNfMessage(string summary, string body, string icon)
- {
- var msg = new NfMessage
+ using (var nf = new Notifier ())
+ using (var ws = new WebSocket ("ws://echo.websocket.org"))
+ //using (var ws = new WebSocket ("wss://echo.websocket.org"))
+ //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/エコー?name=のび太"))
+ //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/チャット?name=のび太"))
{
- Summary = summary,
- Body = body,
- Icon = icon
- };
-
- _msgQ.Enqueue(msg);
- }
-
- public static void Main(string[] args)
- {
- var ts = new ThreadState();
-
- WaitCallback notifyMsg = state =>
- {
- while (ts.Enabled || _msgQ.Count > 0)
- {
- Thread.Sleep(500);
-
- if (_msgQ.Count > 0)
- {
- var msg = (NfMessage)_msgQ.Dequeue();
- #if NOTIFY
- var nf = new Notification(msg.Summary, msg.Body, msg.Icon);
- nf.AddHint("append", "allowed");
- nf.Show();
- #else
- Console.WriteLine("{0}: {1}", msg.Summary, msg.Body);
- #endif
- }
- }
-
- ts.Notification.Set();
- };
-
- ThreadPool.QueueUserWorkItem(notifyMsg);
-
- using (var ws = new WebSocket("ws://echo.websocket.org"))
- //using (var ws = new WebSocket("wss://echo.websocket.org"))
- //using (var ws = new WebSocket("ws://localhost:4649"))
- //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/エコー?name=のび太"))
- //using (var ws = new WebSocket("ws://localhost:4649/Chat"))
- //using (var ws = new WebSocket("ws://localhost:4649/Chat?name=nobita"))
- //using (var ws = new WebSocket("ws://localhost:4649/チャット?name=のび太"))
- {
- ws.OnOpen += (sender, e) =>
- {
- ws.Send("Hi, all!");
- };
+ /* WebSocket events */
+ ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");
ws.OnMessage += (sender, e) =>
- {
- if (!String.IsNullOrEmpty(e.Data))
- {
- enNfMessage("[WebSocket] Message", e.Data, "notification-message-im");
- }
- };
+ nf.Notify (
+ new NotificationMessage () {
+ Summary = "WebSocket Message",
+ Body = e.Data,
+ Icon = "notification-message-im"
+ });
ws.OnError += (sender, e) =>
- {
- enNfMessage("[WebSocket] Error", e.Message, "notification-message-im");
- };
+ nf.Notify (
+ new NotificationMessage () {
+ Summary = "WebSocket Error",
+ Body = e.Message,
+ Icon = "notification-message-im"
+ });
ws.OnClose += (sender, e) =>
- {
- enNfMessage(
- String.Format("[WebSocket] Close({0})", e.Code),
- e.Reason,
- "notification-message-im");
- };
-
- #if DEBUG
+ nf.Notify (
+ new NotificationMessage () {
+ Summary = String.Format ("WebSocket Close ({0})", e.Code),
+ Body = e.Reason,
+ Icon = "notification-message-im"
+ });
+
+#if DEBUG
ws.Log.Level = LogLevel.Trace;
- #endif
+#endif
+
+ // Per-message Compression
//ws.Compression = CompressionMethod.Deflate;
+
+ /* Secure Connection
+ 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
+ };
+ */
+
+ // HTTP Authentication (Basic/Digest)
+ //ws.SetCredentials ("nobita", "password", false); // Digest
+
+ // Origin
//ws.Origin = "http://echo.websocket.org";
- //ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
- //{
- // ws.Log.Debug(String.Format("\n{0}\n{1}", certificate.Issuer, certificate.Subject));
- // return true;
- //};
- //ws.SetCookie(new Cookie("nobita", "\"idiot, gunfighter\""));
- //ws.SetCookie(new Cookie("dora", "tanuki"));
- //ws.SetCredentials ("nobita", "password", false);
- ws.Connect();
- //ws.ConnectAsync();
- //Console.WriteLine("Compression: {0}", ws.Compression);
- Thread.Sleep(500);
- Console.WriteLine("\nType \"exit\" to exit.\n");
+ // Cookies
+ //ws.SetCookie (new Cookie ("nobita", "\"idiot, gunfighter\""));
+ //ws.SetCookie (new Cookie ("dora", "tanuki"));
- string data;
- while (true)
- {
- Thread.Sleep(500);
+ ws.Connect ();
+ //ws.ConnectAsync ();
- Console.Write("> ");
- data = Console.ReadLine();
- if (data == "exit")
- //if (data == "exit" || !ws.IsAlive)
- {
+ Console.WriteLine ("\nType \"exit\" to exit.\n");
+ while (true) {
+ Thread.Sleep (500);
+ Console.Write ("> ");
+ var msg = Console.ReadLine ();
+ if (msg == "exit") {
break;
}
- ws.Send(data);
+ ws.Send (msg);
}
}
-
- ts.Enabled = false;
- ts.Notification.WaitOne();
}
}
}