Skip to content

Commit 9a22fb4

Browse files
committed
Modified Example
1 parent e233e3d commit 9a22fb4

File tree

4 files changed

+174
-130
lines changed

4 files changed

+174
-130
lines changed

Example/Example.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<RootNamespace>Example</RootNamespace>
1111
<AssemblyName>example</AssemblyName>
1212
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
13-
<BaseDirectory>.</BaseDirectory>
1413
</PropertyGroup>
1514
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1615
<DebugSymbols>true</DebugSymbols>
@@ -35,7 +34,7 @@
3534
<DebugType>full</DebugType>
3635
<Optimize>false</Optimize>
3736
<OutputPath>bin\Debug_Ubuntu</OutputPath>
38-
<DefineConstants>DEBUG,NOTIFY</DefineConstants>
37+
<DefineConstants>DEBUG,UBUNTU</DefineConstants>
3938
<ErrorReport>prompt</ErrorReport>
4039
<WarningLevel>4</WarningLevel>
4140
<Externalconsole>true</Externalconsole>
@@ -44,7 +43,7 @@
4443
<DebugType>none</DebugType>
4544
<Optimize>false</Optimize>
4645
<OutputPath>bin\Release_Ubuntu</OutputPath>
47-
<DefineConstants>NOTIFY</DefineConstants>
46+
<DefineConstants>UBUNTU</DefineConstants>
4847
<ErrorReport>prompt</ErrorReport>
4948
<WarningLevel>4</WarningLevel>
5049
<Externalconsole>true</Externalconsole>
@@ -66,5 +65,7 @@
6665
<ItemGroup>
6766
<Compile Include="Program.cs" />
6867
<Compile Include="AssemblyInfo.cs" />
68+
<Compile Include="Notifier.cs" />
69+
<Compile Include="NotificationMessage.cs" />
6970
</ItemGroup>
7071
</Project>

Example/NotificationMessage.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace Example
4+
{
5+
internal class NotificationMessage
6+
{
7+
public NotificationMessage ()
8+
{
9+
}
10+
11+
public string Body {
12+
get; set;
13+
}
14+
15+
public string Icon {
16+
get; set;
17+
}
18+
19+
public string Summary {
20+
get; set;
21+
}
22+
23+
public override string ToString ()
24+
{
25+
return String.Format ("[{0}: {1}]", Summary, Body);
26+
}
27+
}
28+
}

Example/Notifier.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#if UBUNTU
2+
using Notifications;
3+
#endif
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
9+
namespace Example
10+
{
11+
internal class Notifier : IDisposable
12+
{
13+
private bool _enabled;
14+
private Queue<NotificationMessage> _queue;
15+
private ManualResetEvent _waitHandle;
16+
17+
public Notifier ()
18+
{
19+
_enabled = true;
20+
_queue = new Queue<NotificationMessage> ();
21+
_waitHandle = new ManualResetEvent (false);
22+
23+
ThreadPool.QueueUserWorkItem (
24+
state => {
25+
while (_enabled || Count > 0) {
26+
Thread.Sleep (500);
27+
if (Count > 0) {
28+
var msg = dequeue ();
29+
#if UBUNTU
30+
var nf = new Notification (msg.Summary, msg.Body, msg.Icon);
31+
nf.AddHint ("append", "allowed");
32+
nf.Show ();
33+
#else
34+
Console.WriteLine (msg);
35+
#endif
36+
}
37+
}
38+
39+
_waitHandle.Set ();
40+
});
41+
}
42+
43+
public int Count {
44+
get {
45+
lock (((ICollection) _queue).SyncRoot) {
46+
return _queue.Count;
47+
}
48+
}
49+
}
50+
51+
private NotificationMessage dequeue ()
52+
{
53+
lock (((ICollection) _queue).SyncRoot) {
54+
return _queue.Dequeue ();
55+
}
56+
}
57+
58+
public void Close ()
59+
{
60+
_enabled = false;
61+
_waitHandle.WaitOne ();
62+
_waitHandle.Close ();
63+
}
64+
65+
public void Notify (NotificationMessage message)
66+
{
67+
lock (((ICollection) _queue).SyncRoot) {
68+
_queue.Enqueue (message);
69+
}
70+
}
71+
72+
void IDisposable.Dispose ()
73+
{
74+
Close ();
75+
}
76+
}
77+
}

Example/Program.cs

Lines changed: 65 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,92 @@
1-
#if NOTIFY
2-
using Notifications;
3-
#endif
41
using System;
5-
using System.Collections;
6-
using System.Linq;
72
using System.Threading;
83
using WebSocketSharp;
94
using WebSocketSharp.Net;
105

11-
namespace Example {
12-
13-
public struct NfMessage {
14-
15-
public string Summary;
16-
public string Body;
17-
public string Icon;
18-
}
19-
20-
public class ThreadState {
21-
22-
public bool Enabled { get; set; }
23-
public AutoResetEvent Notification { get; private set; }
24-
25-
public ThreadState()
26-
{
27-
Enabled = true;
28-
Notification = new AutoResetEvent(false);
29-
}
30-
}
31-
32-
public class Program {
33-
34-
private static Queue _msgQ = Queue.Synchronized(new Queue());
35-
36-
private static void enNfMessage(string summary, string body, string icon)
37-
{
38-
var msg = new NfMessage
39-
{
40-
Summary = summary,
41-
Body = body,
42-
Icon = icon
43-
};
44-
45-
_msgQ.Enqueue(msg);
46-
}
47-
48-
public static void Main(string[] args)
6+
namespace Example
7+
{
8+
public class Program
9+
{
10+
public static void Main (string [] args)
4911
{
50-
var ts = new ThreadState();
51-
52-
WaitCallback notifyMsg = state =>
12+
using (var nf = new Notifier ())
13+
using (var ws = new WebSocket ("ws://echo.websocket.org"))
14+
//using (var ws = new WebSocket ("wss://echo.websocket.org"))
15+
//using (var ws = new WebSocket ("ws://localhost:4649/Echo"))
16+
//using (var ws = new WebSocket ("wss://localhost:4649/Echo"))
17+
//using (var ws = new WebSocket ("ws://localhost:4649/Echo?name=nobita"))
18+
//using (var ws = new WebSocket ("ws://localhost:4649/エコー?name=のび太"))
19+
//using (var ws = new WebSocket ("ws://localhost:4649/Chat"))
20+
//using (var ws = new WebSocket ("wss://localhost:4649/Chat"))
21+
//using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
22+
//using (var ws = new WebSocket ("ws://localhost:4649/チャット?name=のび太"))
5323
{
54-
while (ts.Enabled || _msgQ.Count > 0)
55-
{
56-
Thread.Sleep(500);
57-
58-
if (_msgQ.Count > 0)
59-
{
60-
var msg = (NfMessage)_msgQ.Dequeue();
61-
#if NOTIFY
62-
var nf = new Notification(msg.Summary, msg.Body, msg.Icon);
63-
nf.AddHint("append", "allowed");
64-
nf.Show();
65-
#else
66-
Console.WriteLine("{0}: {1}", msg.Summary, msg.Body);
67-
#endif
68-
}
69-
}
70-
71-
ts.Notification.Set();
72-
};
73-
74-
ThreadPool.QueueUserWorkItem(notifyMsg);
75-
76-
using (var ws = new WebSocket("ws://echo.websocket.org"))
77-
//using (var ws = new WebSocket("wss://echo.websocket.org"))
78-
//using (var ws = new WebSocket("ws://localhost:4649"))
79-
//using (var ws = new WebSocket("ws://localhost:4649/Echo"))
80-
//using (var ws = new WebSocket("wss://localhost:4649/Echo"))
81-
//using (var ws = new WebSocket("ws://localhost:4649/Echo?name=nobita"))
82-
//using (var ws = new WebSocket("ws://localhost:4649/エコー?name=のび太"))
83-
//using (var ws = new WebSocket("ws://localhost:4649/Chat"))
84-
//using (var ws = new WebSocket("ws://localhost:4649/Chat?name=nobita"))
85-
//using (var ws = new WebSocket("ws://localhost:4649/チャット?name=のび太"))
86-
{
87-
ws.OnOpen += (sender, e) =>
88-
{
89-
ws.Send("Hi, all!");
90-
};
24+
/* WebSocket events */
25+
ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");
9126

9227
ws.OnMessage += (sender, e) =>
93-
{
94-
if (!String.IsNullOrEmpty(e.Data))
95-
{
96-
enNfMessage("[WebSocket] Message", e.Data, "notification-message-im");
97-
}
98-
};
28+
nf.Notify (
29+
new NotificationMessage () {
30+
Summary = "WebSocket Message",
31+
Body = e.Data,
32+
Icon = "notification-message-im"
33+
});
9934

10035
ws.OnError += (sender, e) =>
101-
{
102-
enNfMessage("[WebSocket] Error", e.Message, "notification-message-im");
103-
};
36+
nf.Notify (
37+
new NotificationMessage () {
38+
Summary = "WebSocket Error",
39+
Body = e.Message,
40+
Icon = "notification-message-im"
41+
});
10442

10543
ws.OnClose += (sender, e) =>
106-
{
107-
enNfMessage(
108-
String.Format("[WebSocket] Close({0})", e.Code),
109-
e.Reason,
110-
"notification-message-im");
111-
};
112-
113-
#if DEBUG
44+
nf.Notify (
45+
new NotificationMessage () {
46+
Summary = String.Format ("WebSocket Close ({0})", e.Code),
47+
Body = e.Reason,
48+
Icon = "notification-message-im"
49+
});
50+
51+
#if DEBUG
11452
ws.Log.Level = LogLevel.Trace;
115-
#endif
53+
#endif
54+
55+
// Per-message Compression
11656
//ws.Compression = CompressionMethod.Deflate;
57+
58+
/* Secure Connection
59+
ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
60+
ws.Log.Debug (String.Format ("\n{0}\n{1}", certificate.Issuer, certificate.Subject));
61+
return true; // If the server cert is valid
62+
};
63+
*/
64+
65+
// HTTP Authentication (Basic/Digest)
66+
//ws.SetCredentials ("nobita", "password", false); // Digest
67+
68+
// Origin
11769
//ws.Origin = "http://echo.websocket.org";
118-
//ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
119-
//{
120-
// ws.Log.Debug(String.Format("\n{0}\n{1}", certificate.Issuer, certificate.Subject));
121-
// return true;
122-
//};
123-
//ws.SetCookie(new Cookie("nobita", "\"idiot, gunfighter\""));
124-
//ws.SetCookie(new Cookie("dora", "tanuki"));
125-
//ws.SetCredentials ("nobita", "password", false);
126-
ws.Connect();
127-
//ws.ConnectAsync();
128-
//Console.WriteLine("Compression: {0}", ws.Compression);
12970

130-
Thread.Sleep(500);
131-
Console.WriteLine("\nType \"exit\" to exit.\n");
71+
// Cookies
72+
//ws.SetCookie (new Cookie ("nobita", "\"idiot, gunfighter\""));
73+
//ws.SetCookie (new Cookie ("dora", "tanuki"));
13274

133-
string data;
134-
while (true)
135-
{
136-
Thread.Sleep(500);
75+
ws.Connect ();
76+
//ws.ConnectAsync ();
13777

138-
Console.Write("> ");
139-
data = Console.ReadLine();
140-
if (data == "exit")
141-
//if (data == "exit" || !ws.IsAlive)
142-
{
78+
Console.WriteLine ("\nType \"exit\" to exit.\n");
79+
while (true) {
80+
Thread.Sleep (500);
81+
Console.Write ("> ");
82+
var msg = Console.ReadLine ();
83+
if (msg == "exit") {
14384
break;
14485
}
14586

146-
ws.Send(data);
87+
ws.Send (msg);
14788
}
14889
}
149-
150-
ts.Enabled = false;
151-
ts.Notification.WaitOne();
15290
}
15391
}
15492
}

0 commit comments

Comments
 (0)