Supports RFC 6455
This commit is contained in:
27
Example/AssemblyInfo.cs
Normal file
27
Example/AssemblyInfo.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// Information about this assembly is defined by the following attributes.
|
||||
// Change them to the values specific to your project.
|
||||
|
||||
[assembly: AssemblyTitle("Example")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("")]
|
||||
[assembly: AssemblyCopyright("sta")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
||||
|
||||
[assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
// The following attributes are used to specify the signing key for the assembly,
|
||||
// if desired. See the Mono documentation for more information about signing.
|
||||
|
||||
//[assembly: AssemblyDelaySign(false)]
|
||||
//[assembly: AssemblyKeyFile("")]
|
||||
|
127
Example/Program.cs
Normal file
127
Example/Program.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
#if NOTIFY
|
||||
using Notifications;
|
||||
#endif
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using WebSocketSharp;
|
||||
|
||||
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)
|
||||
{
|
||||
ThreadState ts = new ThreadState();
|
||||
|
||||
WaitCallback notifyMsg = state =>
|
||||
{
|
||||
while (ts.Enabled)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
|
||||
if (_msgQ.Count > 0)
|
||||
{
|
||||
NfMessage msg = (NfMessage)_msgQ.Dequeue();
|
||||
#if NOTIFY
|
||||
Notification 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 (WebSocket ws = new WebSocket("ws://echo.websocket.org", "echo"))
|
||||
//using (WebSocket ws = new WebSocket("wss://echo.websocket.org", "echo"))
|
||||
{
|
||||
ws.OnOpen += (sender, e) =>
|
||||
{
|
||||
ws.Send("Hi, all!");
|
||||
};
|
||||
|
||||
ws.OnMessage += (sender, e) =>
|
||||
{
|
||||
enNfMessage("[WebSocket] Message", e.Data, "notification-message-im");
|
||||
};
|
||||
|
||||
ws.OnError += (sender, e) =>
|
||||
{
|
||||
enNfMessage("[WebSocket] Error", e.Data, "notification-message-im");
|
||||
};
|
||||
|
||||
ws.OnClose += (sender, e) =>
|
||||
{
|
||||
enNfMessage(
|
||||
String.Format("[WebSocket] Close({0}:{1})", (ushort)e.Code, e.Code),
|
||||
e.Reason,
|
||||
"notification-message-im");
|
||||
};
|
||||
|
||||
ws.Connect();
|
||||
|
||||
Thread.Sleep(500);
|
||||
Console.WriteLine("\nType \"exit\" to exit.\n");
|
||||
|
||||
string data;
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
|
||||
Console.Write("> ");
|
||||
data = Console.ReadLine();
|
||||
if (data == "exit")
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ws.Send(data);
|
||||
}
|
||||
}
|
||||
|
||||
ts.Enabled = false;
|
||||
ts.Notification.WaitOne();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user