Modified Example

This commit is contained in:
sta
2014-03-11 16:41:43 +09:00
parent e233e3def3
commit 9a22fb4235
4 changed files with 174 additions and 130 deletions

View File

@@ -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();
}
}
}