websocket-sharp/Example/Program.cs

155 lines
4.1 KiB
C#
Raw Normal View History

2012-07-31 09:36:52 +08:00
#if NOTIFY
using Notifications;
#endif
using System;
using System.Collections;
2013-04-06 15:21:41 +08:00
using System.Linq;
2012-07-31 09:36:52 +08:00
using System.Threading;
using WebSocketSharp;
2013-04-06 15:21:41 +08:00
using WebSocketSharp.Net;
2012-07-31 09:36:52 +08:00
2013-07-19 16:29:58 +08:00
namespace Example {
public struct NfMessage {
2012-07-31 09:36:52 +08:00
public string Summary;
public string Body;
public string Icon;
}
2013-07-19 16:29:58 +08:00
public class ThreadState {
2012-07-31 09:36:52 +08:00
public bool Enabled { get; set; }
public AutoResetEvent Notification { get; private set; }
public ThreadState()
{
2013-07-19 16:29:58 +08:00
Enabled = true;
2012-07-31 09:36:52 +08:00
Notification = new AutoResetEvent(false);
}
}
2013-07-19 16:29:58 +08:00
public class Program {
2012-07-31 09:36:52 +08:00
private static Queue _msgQ = Queue.Synchronized(new Queue());
private static void enNfMessage(string summary, string body, string icon)
{
var msg = new NfMessage
{
Summary = summary,
2013-07-19 16:29:58 +08:00
Body = body,
Icon = icon
2012-07-31 09:36:52 +08:00
};
_msgQ.Enqueue(msg);
}
public static void Main(string[] args)
{
2013-07-19 16:29:58 +08:00
var ts = new ThreadState();
2012-07-31 09:36:52 +08:00
WaitCallback notifyMsg = state =>
{
while (ts.Enabled || _msgQ.Count > 0)
2012-07-31 09:36:52 +08:00
{
Thread.Sleep(500);
if (_msgQ.Count > 0)
{
2013-07-19 16:29:58 +08:00
var msg = (NfMessage)_msgQ.Dequeue();
2012-07-31 09:36:52 +08:00
#if NOTIFY
2013-07-19 16:29:58 +08:00
var nf = new Notification(msg.Summary, msg.Body, msg.Icon);
2012-07-31 09:36:52 +08:00
nf.AddHint("append", "allowed");
nf.Show();
#else
Console.WriteLine("{0}: {1}", msg.Summary, msg.Body);
#endif
}
}
ts.Notification.Set();
};
ThreadPool.QueueUserWorkItem(notifyMsg);
2014-02-06 12:50:27 +08:00
using (var ws = new WebSocket("ws://echo.websocket.org"))
//using (var ws = new WebSocket("wss://echo.websocket.org"))
2013-07-19 16:29:58 +08:00
//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=のび太"))
2012-07-31 09:36:52 +08:00
{
ws.OnOpen += (sender, e) =>
{
ws.Send("Hi, all!");
};
ws.OnMessage += (sender, e) =>
{
2012-08-19 15:41:59 +08:00
if (!String.IsNullOrEmpty(e.Data))
{
enNfMessage("[WebSocket] Message", e.Data, "notification-message-im");
}
2012-07-31 09:36:52 +08:00
};
ws.OnError += (sender, e) =>
{
2012-08-04 14:51:31 +08:00
enNfMessage("[WebSocket] Error", e.Message, "notification-message-im");
2012-07-31 09:36:52 +08:00
};
ws.OnClose += (sender, e) =>
{
enNfMessage(
String.Format("[WebSocket] Close({0})", e.Code),
2012-07-31 09:36:52 +08:00
e.Reason,
"notification-message-im");
};
2013-07-15 19:42:55 +08:00
#if DEBUG
ws.Log.Level = LogLevel.Trace;
2013-07-15 19:42:55 +08:00
#endif
2013-07-19 16:29:58 +08:00
//ws.Compression = CompressionMethod.DEFLATE;
//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\""));
2013-04-06 15:21:41 +08:00
//ws.SetCookie(new Cookie("dora", "tanuki"));
//ws.SetCredentials ("nobita", "password", false);
2012-07-31 09:36:52 +08:00
ws.Connect();
//ws.ConnectAsync();
2013-04-25 01:31:38 +08:00
//Console.WriteLine("Compression: {0}", ws.Compression);
2012-07-31 09:36:52 +08:00
Thread.Sleep(500);
Console.WriteLine("\nType \"exit\" to exit.\n");
string data;
while (true)
{
Thread.Sleep(500);
Console.Write("> ");
data = Console.ReadLine();
2012-08-19 15:41:59 +08:00
if (data == "exit")
//if (data == "exit" || !ws.IsAlive)
2012-07-31 09:36:52 +08:00
{
break;
}
ws.Send(data);
}
}
ts.Enabled = false;
ts.Notification.WaitOne();
}
}
}