diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index fdabc879..12176b54 100755 Binary files a/Example/bin/Debug/example.exe and b/Example/bin/Debug/example.exe differ diff --git a/Example/bin/Debug/example.exe.mdb b/Example/bin/Debug/example.exe.mdb index c89a12e6..9c709603 100644 Binary files a/Example/bin/Debug/example.exe.mdb and b/Example/bin/Debug/example.exe.mdb differ diff --git a/Example/bin/Debug/websocket-sharp.dll b/Example/bin/Debug/websocket-sharp.dll index 3f8e338e..9d53da1e 100755 Binary files a/Example/bin/Debug/websocket-sharp.dll and b/Example/bin/Debug/websocket-sharp.dll differ diff --git a/Example/bin/Debug/websocket-sharp.dll.mdb b/Example/bin/Debug/websocket-sharp.dll.mdb index 74b16aeb..00e14f4d 100644 Binary files a/Example/bin/Debug/websocket-sharp.dll.mdb and b/Example/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index ac82b872..28ae917b 100755 Binary files a/Example/bin/Debug_Ubuntu/example.exe and b/Example/bin/Debug_Ubuntu/example.exe differ diff --git a/Example/bin/Debug_Ubuntu/example.exe.mdb b/Example/bin/Debug_Ubuntu/example.exe.mdb index 6b2a4e28..7cb3c30c 100644 Binary files a/Example/bin/Debug_Ubuntu/example.exe.mdb and b/Example/bin/Debug_Ubuntu/example.exe.mdb differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll index a5dd86d4..1156effe 100755 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index be4a95cf..322f211e 100644 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Release/example.exe b/Example/bin/Release/example.exe index 26907ea1..971cc444 100755 Binary files a/Example/bin/Release/example.exe and b/Example/bin/Release/example.exe differ diff --git a/Example/bin/Release/websocket-sharp.dll b/Example/bin/Release/websocket-sharp.dll index 05666cd7..2e922cf2 100755 Binary files a/Example/bin/Release/websocket-sharp.dll and b/Example/bin/Release/websocket-sharp.dll differ diff --git a/Example/bin/Release_Ubuntu/example.exe b/Example/bin/Release_Ubuntu/example.exe index e7c48688..57ac63b9 100755 Binary files a/Example/bin/Release_Ubuntu/example.exe and b/Example/bin/Release_Ubuntu/example.exe differ diff --git a/Example/bin/Release_Ubuntu/websocket-sharp.dll b/Example/bin/Release_Ubuntu/websocket-sharp.dll index b6d8b26e..c4696f93 100755 Binary files a/Example/bin/Release_Ubuntu/websocket-sharp.dll and b/Example/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/example1.exe b/Example1/bin/Debug/example1.exe index e3b24224..366b2c8b 100755 Binary files a/Example1/bin/Debug/example1.exe and b/Example1/bin/Debug/example1.exe differ diff --git a/Example1/bin/Debug/example1.exe.mdb b/Example1/bin/Debug/example1.exe.mdb index c5ad251d..aab332e1 100644 Binary files a/Example1/bin/Debug/example1.exe.mdb and b/Example1/bin/Debug/example1.exe.mdb differ diff --git a/Example1/bin/Debug/websocket-sharp.dll b/Example1/bin/Debug/websocket-sharp.dll index 3f8e338e..9d53da1e 100755 Binary files a/Example1/bin/Debug/websocket-sharp.dll and b/Example1/bin/Debug/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/websocket-sharp.dll.mdb b/Example1/bin/Debug/websocket-sharp.dll.mdb index 74b16aeb..00e14f4d 100644 Binary files a/Example1/bin/Debug/websocket-sharp.dll.mdb and b/Example1/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index fb1065e2..e37424ef 100755 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe and b/Example1/bin/Debug_Ubuntu/example1.exe differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe.mdb b/Example1/bin/Debug_Ubuntu/example1.exe.mdb index 25c086e5..cf26b5f9 100644 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe.mdb and b/Example1/bin/Debug_Ubuntu/example1.exe.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll index a5dd86d4..1156effe 100755 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index be4a95cf..322f211e 100644 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Release/example1.exe b/Example1/bin/Release/example1.exe index 1b6b6d9e..027501ad 100755 Binary files a/Example1/bin/Release/example1.exe and b/Example1/bin/Release/example1.exe differ diff --git a/Example1/bin/Release/websocket-sharp.dll b/Example1/bin/Release/websocket-sharp.dll index 05666cd7..2e922cf2 100755 Binary files a/Example1/bin/Release/websocket-sharp.dll and b/Example1/bin/Release/websocket-sharp.dll differ diff --git a/Example1/bin/Release_Ubuntu/example1.exe b/Example1/bin/Release_Ubuntu/example1.exe index fb9f8fb5..855ed69b 100755 Binary files a/Example1/bin/Release_Ubuntu/example1.exe and b/Example1/bin/Release_Ubuntu/example1.exe differ diff --git a/Example1/bin/Release_Ubuntu/websocket-sharp.dll b/Example1/bin/Release_Ubuntu/websocket-sharp.dll index b6d8b26e..c4696f93 100755 Binary files a/Example1/bin/Release_Ubuntu/websocket-sharp.dll and b/Example1/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/Chat.cs b/Example2/Chat.cs index c13b37f8..48b444c8 100644 --- a/Example2/Chat.cs +++ b/Example2/Chat.cs @@ -8,7 +8,7 @@ namespace Example2 { protected override void onMessage(object sender, MessageEventArgs e) { - Server.Send(e.Data); + Publish(e.Data); } } } diff --git a/Example2/Example2.pidb b/Example2/Example2.pidb index df6f1c68..4c5aa484 100644 Binary files a/Example2/Example2.pidb and b/Example2/Example2.pidb differ diff --git a/Example2/Program.cs b/Example2/Program.cs index d55d03d0..b16f2e1c 100644 --- a/Example2/Program.cs +++ b/Example2/Program.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using WebSocketSharp.Server; namespace Example2 @@ -13,7 +12,8 @@ namespace Example2 wssv.Start(); Console.WriteLine( - "WebSocket Server ({0}) listening on address: {1} port: {2}\n", wssv.Url, wssv.Address, wssv.Port); + "WebSocket Server (url: {0})\n listening on address: {1} port: {2}\n", + wssv.Url, wssv.Address, wssv.Port); Console.WriteLine("Press any key to stop server..."); Console.ReadLine(); diff --git a/Example2/bin/Debug/example2.exe b/Example2/bin/Debug/example2.exe index f8e9e339..ccf9d1bc 100755 Binary files a/Example2/bin/Debug/example2.exe and b/Example2/bin/Debug/example2.exe differ diff --git a/Example2/bin/Debug/example2.exe.mdb b/Example2/bin/Debug/example2.exe.mdb index 60afd932..533d5529 100644 Binary files a/Example2/bin/Debug/example2.exe.mdb and b/Example2/bin/Debug/example2.exe.mdb differ diff --git a/Example2/bin/Debug/websocket-sharp.dll b/Example2/bin/Debug/websocket-sharp.dll index 3f8e338e..9d53da1e 100755 Binary files a/Example2/bin/Debug/websocket-sharp.dll and b/Example2/bin/Debug/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/websocket-sharp.dll.mdb b/Example2/bin/Debug/websocket-sharp.dll.mdb index 74b16aeb..00e14f4d 100644 Binary files a/Example2/bin/Debug/websocket-sharp.dll.mdb and b/Example2/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index 3910b9a3..f204475b 100755 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe and b/Example2/bin/Debug_Ubuntu/example2.exe differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe.mdb b/Example2/bin/Debug_Ubuntu/example2.exe.mdb index 293d0400..6f6f706b 100644 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe.mdb and b/Example2/bin/Debug_Ubuntu/example2.exe.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll index a5dd86d4..1156effe 100755 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index be4a95cf..322f211e 100644 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Release/example2.exe b/Example2/bin/Release/example2.exe index a1686c1b..559a0caf 100755 Binary files a/Example2/bin/Release/example2.exe and b/Example2/bin/Release/example2.exe differ diff --git a/Example2/bin/Release/websocket-sharp.dll b/Example2/bin/Release/websocket-sharp.dll index 05666cd7..2e922cf2 100755 Binary files a/Example2/bin/Release/websocket-sharp.dll and b/Example2/bin/Release/websocket-sharp.dll differ diff --git a/Example2/bin/Release_Ubuntu/example2.exe b/Example2/bin/Release_Ubuntu/example2.exe index 33a5f048..e4cc328b 100755 Binary files a/Example2/bin/Release_Ubuntu/example2.exe and b/Example2/bin/Release_Ubuntu/example2.exe differ diff --git a/Example2/bin/Release_Ubuntu/websocket-sharp.dll b/Example2/bin/Release_Ubuntu/websocket-sharp.dll index b6d8b26e..c4696f93 100755 Binary files a/Example2/bin/Release_Ubuntu/websocket-sharp.dll and b/Example2/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/README.md b/README.md index 47222c66..b9db99d6 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Required namespaces. #### Step 2 #### -Creating instance of `WebSocket` class. +Creating a instance of `WebSocket` class. using (WebSocket ws = new WebSocket("ws://example.com")) { @@ -126,14 +126,15 @@ Required namespace. using WebSocketSharp.Server; -`WebSocketServer` class exists in `WebSocketSharp.Server` namespace. +`WebSocketServer` class and `WebSocketService` class exist in `WebSocketSharp.Server` namespace. #### Step 2 #### -Creating a class that inherits `WebSocketService`. +Creating a class that inherits `WebSocketService` class. For example, if you want to provide the echo service, + ```cs using System; using WebSocketSharp; using WebSocketSharp.Server; @@ -145,9 +146,11 @@ For example, if you want to provide the echo service, Send(e.Data); } } + ``` For example, if you want to provide the chat service, + ```cs using System; using WebSocketSharp; using WebSocketSharp.Server; @@ -156,19 +159,20 @@ For example, if you want to provide the chat service, { protected override void onMessage(object sender, MessageEventArgs e) { - Server.Send(e.Data); + Publish(e.Data); } } + ``` #### Step 3 #### -Creating instance of `WebSocketServer` class. +Creating a instance of `WebSocketServer` class. var wssv = new WebSocketServer("ws://example.com:4649"); Type of `T` inherits `WebSocketService` class, so you can use a class that was created in **Step 2**. -If you set WebSocket url without port number, `WebSocketServer` set 80 or 443 to port number automatically. +If you set WebSocket url without port number, `WebSocketServer` set **80** or **443** to port number automatically. So it is necessary to run with root permission. $ sudo mono example2.exe diff --git a/websocket-sharp.userprefs b/websocket-sharp.userprefs index 4f01e372..8a063dbf 100644 --- a/websocket-sharp.userprefs +++ b/websocket-sharp.userprefs @@ -1,5 +1,5 @@  - + diff --git a/websocket-sharp/Server/IWebSocketServer.cs b/websocket-sharp/Server/IWebSocketServer.cs index eeac2226..42eb5111 100644 --- a/websocket-sharp/Server/IWebSocketServer.cs +++ b/websocket-sharp/Server/IWebSocketServer.cs @@ -34,12 +34,11 @@ namespace WebSocketSharp.Server public interface IWebSocketServer { void AddService(WebSocketService service); - void CloseService(); - void CloseService(CloseStatusCode code, string reason); + void CloseServices(CloseStatusCode code, string reason); void Ping(string data); + void Publish(byte[] data); + void Publish(string data); void RemoveService(WebSocketService service); - void Send(byte[] data); - void Send(string data); void Start(); void Stop(); } diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index 51186093..5e8989e4 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -173,12 +173,12 @@ namespace WebSocketSharp.Server _services.Add(service); } - public void CloseService() + public void CloseServices() { - CloseService(CloseStatusCode.NORMAL, String.Empty); + CloseServices(CloseStatusCode.NORMAL, String.Empty); } - public void CloseService(CloseStatusCode code, string reason) + public void CloseServices(CloseStatusCode code, string reason) { lock (_services.SyncRoot) { @@ -189,6 +189,11 @@ namespace WebSocketSharp.Server } } + public void Ping() + { + Ping(String.Empty); + } + public void Ping(string data) { WaitCallback broadcast = (state) => @@ -204,12 +209,7 @@ namespace WebSocketSharp.Server ThreadPool.QueueUserWorkItem(broadcast); } - public void RemoveService(WebSocketService service) - { - _services.Remove(service); - } - - public void Send(byte[] data) + public void Publish(byte[] data) { WaitCallback broadcast = (state) => { @@ -224,7 +224,7 @@ namespace WebSocketSharp.Server ThreadPool.QueueUserWorkItem(broadcast); } - public void Send(string data) + public void Publish(string data) { WaitCallback broadcast = (state) => { @@ -239,6 +239,11 @@ namespace WebSocketSharp.Server ThreadPool.QueueUserWorkItem(broadcast); } + public void RemoveService(WebSocketService service) + { + _services.Remove(service); + } + public void Start() { _tcpListener.Start(); @@ -248,7 +253,7 @@ namespace WebSocketSharp.Server public void Stop() { _tcpListener.Stop(); - CloseService(); + CloseServices(); } #endregion diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index fee0f19f..9385bd5a 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -33,10 +33,16 @@ namespace WebSocketSharp.Server { public abstract class WebSocketService { - #region Properties + #region Private Fields - public IWebSocketServer Server { get; private set; } - public WebSocket Socket { get; private set; } + private IWebSocketServer _server; + private WebSocket _socket; + + #endregion + + #region Property + + public bool IsBinded { get; private set; } #endregion @@ -44,6 +50,7 @@ namespace WebSocketSharp.Server public WebSocketService() { + IsBinded = false; } #endregion @@ -52,9 +59,14 @@ namespace WebSocketSharp.Server private void defaultBind() { - Socket.OnOpen += (sender, e) => + _socket.OnOpen += (sender, e) => + { + _server.AddService(this); + }; + + _socket.OnClose += (sender, e) => { - Server.AddService(this); + _server.RemoveService(this); }; } @@ -76,7 +88,6 @@ namespace WebSocketSharp.Server protected virtual void onClose(object sender, CloseEventArgs e) { - Server.RemoveService(this); } #endregion @@ -85,44 +96,71 @@ namespace WebSocketSharp.Server public void Bind(IWebSocketServer server, WebSocket socket) { - Server = server; - Socket = socket; + _server = server; + _socket = socket; defaultBind(); - Socket.OnOpen += onOpen; - Socket.OnMessage += onMessage; - Socket.OnError += onError; - Socket.OnClose += onClose; + _socket.OnOpen += onOpen; + _socket.OnMessage += onMessage; + _socket.OnError += onError; + _socket.OnClose += onClose; + + IsBinded = true; + } + + public void BPing() + { + BPing(String.Empty); + } + + public void BPing(string data) + { + if (IsBinded) _server.Ping(data); } public void Close() { - Socket.Close(); + if (IsBinded) _socket.Close(); } public void Close(CloseStatusCode code, string reason) { - Socket.Close(code, reason); + if (IsBinded) _socket.Close(code, reason); } public void Open() { - Socket.Connect(); + if (IsBinded) _socket.Connect(); + } + + public void Ping() + { + if (IsBinded) _socket.Ping(); } public void Ping(string data) { - Socket.Ping(data); + if (IsBinded) _socket.Ping(data); + } + + public void Publish(byte[] data) + { + if (IsBinded) _server.Publish(data); + } + + public void Publish(string data) + { + if (IsBinded) _server.Publish(data); } public void Send(byte[] data) { - Socket.Send(data); + if (IsBinded) _socket.Send(data); } public void Send(string data) { - Socket.Send(data); + if (IsBinded) _socket.Send(data); } #endregion diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index 3f8e338e..9d53da1e 100755 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll and b/websocket-sharp/bin/Debug/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb index 74b16aeb..00e14f4d 100644 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index a5dd86d4..1156effe 100755 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index be4a95cf..322f211e 100644 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Release/websocket-sharp.dll b/websocket-sharp/bin/Release/websocket-sharp.dll index 05666cd7..2e922cf2 100755 Binary files a/websocket-sharp/bin/Release/websocket-sharp.dll and b/websocket-sharp/bin/Release/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll index b6d8b26e..c4696f93 100755 Binary files a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index 7405464a..f01465ff 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ