|
1 |
| -#if NOTIFY |
2 |
| -using Notifications; |
3 |
| -#endif |
4 | 1 | using System;
|
5 |
| -using System.Collections; |
6 |
| -using System.Linq; |
7 | 2 | using System.Threading;
|
8 | 3 | using WebSocketSharp;
|
9 | 4 | using WebSocketSharp.Net;
|
10 | 5 |
|
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) |
49 | 11 | {
|
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=のび太")) |
53 | 23 | {
|
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!"); |
91 | 26 |
|
92 | 27 | 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 | + }); |
99 | 34 |
|
100 | 35 | 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 | + }); |
104 | 42 |
|
105 | 43 | 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 |
114 | 52 | ws.Log.Level = LogLevel.Trace;
|
115 |
| - #endif |
| 53 | +#endif |
| 54 | + |
| 55 | + // Per-message Compression |
116 | 56 | //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 |
117 | 69 | //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); |
129 | 70 |
|
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")); |
132 | 74 |
|
133 |
| - string data; |
134 |
| - while (true) |
135 |
| - { |
136 |
| - Thread.Sleep(500); |
| 75 | + ws.Connect (); |
| 76 | + //ws.ConnectAsync (); |
137 | 77 |
|
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") { |
143 | 84 | break;
|
144 | 85 | }
|
145 | 86 |
|
146 |
| - ws.Send(data); |
| 87 | + ws.Send (msg); |
147 | 88 | }
|
148 | 89 | }
|
149 |
| - |
150 |
| - ts.Enabled = false; |
151 |
| - ts.Notification.WaitOne(); |
152 | 90 | }
|
153 | 91 | }
|
154 | 92 | }
|
0 commit comments