Modified Example
This commit is contained in:
parent
e233e3def3
commit
9a22fb4235
@ -10,7 +10,6 @@
|
||||
<RootNamespace>Example</RootNamespace>
|
||||
<AssemblyName>example</AssemblyName>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
<BaseDirectory>.</BaseDirectory>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@ -35,7 +34,7 @@
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug_Ubuntu</OutputPath>
|
||||
<DefineConstants>DEBUG,NOTIFY</DefineConstants>
|
||||
<DefineConstants>DEBUG,UBUNTU</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
@ -44,7 +43,7 @@
|
||||
<DebugType>none</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Release_Ubuntu</OutputPath>
|
||||
<DefineConstants>NOTIFY</DefineConstants>
|
||||
<DefineConstants>UBUNTU</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
@ -66,5 +65,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="Notifier.cs" />
|
||||
<Compile Include="NotificationMessage.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
28
Example/NotificationMessage.cs
Normal file
28
Example/NotificationMessage.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
77
Example/Notifier.cs
Normal file
77
Example/Notifier.cs
Normal file
@ -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.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");
|
||||
};
|
||||
nf.Notify (
|
||||
new NotificationMessage () {
|
||||
Summary = String.Format ("WebSocket Close ({0})", e.Code),
|
||||
Body = e.Reason,
|
||||
Icon = "notification-message-im"
|
||||
});
|
||||
|
||||
#if DEBUG
|
||||
#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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user