
4 changed files with 174 additions and 130 deletions
-
7Example/Example.csproj
-
28Example/NotificationMessage.cs
-
77Example/Notifier.cs
-
192Example/Program.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); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -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<NotificationMessage> _queue; |
||||
|
private ManualResetEvent _waitHandle; |
||||
|
|
||||
|
public Notifier () |
||||
|
{ |
||||
|
_enabled = true; |
||||
|
_queue = new Queue<NotificationMessage> (); |
||||
|
_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 (); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -1,154 +1,92 @@ |
|||||
#if NOTIFY
|
|
||||
using Notifications; |
|
||||
#endif
|
|
||||
using System; |
using System; |
||||
using System.Collections; |
|
||||
using System.Linq; |
|
||||
using System.Threading; |
using System.Threading; |
||||
using WebSocketSharp; |
using WebSocketSharp; |
||||
using WebSocketSharp.Net; |
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() |
|
||||
{ |
|
||||
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 |
|
||||
{ |
|
||||
Summary = summary, |
|
||||
Body = body, |
|
||||
Icon = icon |
|
||||
}; |
|
||||
|
|
||||
_msgQ.Enqueue(msg); |
|
||||
} |
|
||||
|
|
||||
public static void Main(string[] args) |
|
||||
|
namespace Example |
||||
|
{ |
||||
|
public class Program |
||||
|
{ |
||||
|
public static void Main (string [] args) |
||||
{ |
{ |
||||
var ts = new ThreadState(); |
|
||||
|
|
||||
WaitCallback notifyMsg = state => |
|
||||
|
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=のび太"))
|
||||
{ |
{ |
||||
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) => |
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) => |
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) => |
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; |
ws.Log.Level = LogLevel.Trace; |
||||
#endif
|
|
||||
|
#endif
|
||||
|
|
||||
|
// Per-message Compression
|
||||
//ws.Compression = CompressionMethod.Deflate;
|
//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.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; |
break; |
||||
} |
} |
||||
|
|
||||
ws.Send(data); |
|
||||
|
ws.Send (msg); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
ts.Enabled = false; |
|
||||
ts.Notification.WaitOne(); |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue