Modified Example
This commit is contained in:
parent
e233e3def3
commit
9a22fb4235
@ -10,7 +10,6 @@
|
|||||||
<RootNamespace>Example</RootNamespace>
|
<RootNamespace>Example</RootNamespace>
|
||||||
<AssemblyName>example</AssemblyName>
|
<AssemblyName>example</AssemblyName>
|
||||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||||
<BaseDirectory>.</BaseDirectory>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
@ -35,7 +34,7 @@
|
|||||||
<DebugType>full</DebugType>
|
<DebugType>full</DebugType>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>false</Optimize>
|
||||||
<OutputPath>bin\Debug_Ubuntu</OutputPath>
|
<OutputPath>bin\Debug_Ubuntu</OutputPath>
|
||||||
<DefineConstants>DEBUG,NOTIFY</DefineConstants>
|
<DefineConstants>DEBUG,UBUNTU</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
<Externalconsole>true</Externalconsole>
|
<Externalconsole>true</Externalconsole>
|
||||||
@ -44,7 +43,7 @@
|
|||||||
<DebugType>none</DebugType>
|
<DebugType>none</DebugType>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>false</Optimize>
|
||||||
<OutputPath>bin\Release_Ubuntu</OutputPath>
|
<OutputPath>bin\Release_Ubuntu</OutputPath>
|
||||||
<DefineConstants>NOTIFY</DefineConstants>
|
<DefineConstants>UBUNTU</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
<Externalconsole>true</Externalconsole>
|
<Externalconsole>true</Externalconsole>
|
||||||
@ -66,5 +65,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="AssemblyInfo.cs" />
|
<Compile Include="AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Notifier.cs" />
|
||||||
|
<Compile Include="NotificationMessage.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</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;
|
||||||
using System.Collections;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using WebSocketSharp;
|
using WebSocketSharp;
|
||||||
using WebSocketSharp.Net;
|
using WebSocketSharp.Net;
|
||||||
|
|
||||||
namespace Example {
|
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;
|
public class Program
|
||||||
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)
|
public static void Main (string [] args)
|
||||||
{
|
{
|
||||||
var ts = new ThreadState();
|
using (var nf = new Notifier ())
|
||||||
|
|
||||||
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 ("ws://echo.websocket.org"))
|
||||||
//using (var ws = new WebSocket ("wss://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 ("ws://localhost:4649/Echo"))
|
||||||
//using (var ws = new WebSocket ("wss://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/Echo?name=nobita"))
|
||||||
//using (var ws = new WebSocket ("ws://localhost:4649/エコー?name=のび太"))
|
//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"))
|
||||||
|
//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/Chat?name=nobita"))
|
||||||
//using (var ws = new WebSocket ("ws://localhost:4649/チャット?name=のび太"))
|
//using (var ws = new WebSocket ("ws://localhost:4649/チャット?name=のび太"))
|
||||||
{
|
{
|
||||||
ws.OnOpen += (sender, e) =>
|
/* WebSocket events */
|
||||||
{
|
ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");
|
||||||
ws.Send("Hi, all!");
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.OnMessage += (sender, e) =>
|
ws.OnMessage += (sender, e) =>
|
||||||
{
|
nf.Notify (
|
||||||
if (!String.IsNullOrEmpty(e.Data))
|
new NotificationMessage () {
|
||||||
{
|
Summary = "WebSocket Message",
|
||||||
enNfMessage("[WebSocket] Message", e.Data, "notification-message-im");
|
Body = e.Data,
|
||||||
}
|
Icon = "notification-message-im"
|
||||||
};
|
});
|
||||||
|
|
||||||
ws.OnError += (sender, e) =>
|
ws.OnError += (sender, e) =>
|
||||||
{
|
nf.Notify (
|
||||||
enNfMessage("[WebSocket] Error", e.Message, "notification-message-im");
|
new NotificationMessage () {
|
||||||
};
|
Summary = "WebSocket Error",
|
||||||
|
Body = e.Message,
|
||||||
|
Icon = "notification-message-im"
|
||||||
|
});
|
||||||
|
|
||||||
ws.OnClose += (sender, e) =>
|
ws.OnClose += (sender, e) =>
|
||||||
{
|
nf.Notify (
|
||||||
enNfMessage(
|
new NotificationMessage () {
|
||||||
String.Format("[WebSocket] Close({0})", e.Code),
|
Summary = String.Format ("WebSocket Close ({0})", e.Code),
|
||||||
e.Reason,
|
Body = e.Reason,
|
||||||
"notification-message-im");
|
Icon = "notification-message-im"
|
||||||
};
|
});
|
||||||
|
|
||||||
#if DEBUG
|
#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) =>
|
|
||||||
//{
|
// Cookies
|
||||||
// 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 ("nobita", "\"idiot, gunfighter\""));
|
||||||
//ws.SetCookie (new Cookie ("dora", "tanuki"));
|
//ws.SetCookie (new Cookie ("dora", "tanuki"));
|
||||||
//ws.SetCredentials ("nobita", "password", false);
|
|
||||||
ws.Connect ();
|
ws.Connect ();
|
||||||
//ws.ConnectAsync ();
|
//ws.ConnectAsync ();
|
||||||
//Console.WriteLine("Compression: {0}", ws.Compression);
|
|
||||||
|
|
||||||
Thread.Sleep(500);
|
|
||||||
Console.WriteLine ("\nType \"exit\" to exit.\n");
|
Console.WriteLine ("\nType \"exit\" to exit.\n");
|
||||||
|
while (true) {
|
||||||
string data;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
Thread.Sleep (500);
|
Thread.Sleep (500);
|
||||||
|
|
||||||
Console.Write ("> ");
|
Console.Write ("> ");
|
||||||
data = Console.ReadLine();
|
var msg = Console.ReadLine ();
|
||||||
if (data == "exit")
|
if (msg == "exit") {
|
||||||
//if (data == "exit" || !ws.IsAlive)
|
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.Send(data);
|
ws.Send (msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ts.Enabled = false;
|
|
||||||
ts.Notification.WaitOne();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user