Fixed WebSocketService

This commit is contained in:
sta 2012-08-08 11:10:02 +09:00
parent 42461dfc52
commit 64d12eb5cf
51 changed files with 112 additions and 66 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -8,7 +8,7 @@ namespace Example2
{ {
protected override void onMessage(object sender, MessageEventArgs e) protected override void onMessage(object sender, MessageEventArgs e)
{ {
Server.Send(e.Data); Publish(e.Data);
} }
} }
} }

Binary file not shown.

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Threading;
using WebSocketSharp.Server; using WebSocketSharp.Server;
namespace Example2 namespace Example2
@ -13,7 +12,8 @@ namespace Example2
wssv.Start(); wssv.Start();
Console.WriteLine( 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.WriteLine("Press any key to stop server...");
Console.ReadLine(); Console.ReadLine();

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -17,7 +17,7 @@ Required namespaces.
#### Step 2 #### #### Step 2 ####
Creating instance of `WebSocket` class. Creating a instance of `WebSocket` class.
using (WebSocket ws = new WebSocket("ws://example.com")) using (WebSocket ws = new WebSocket("ws://example.com"))
{ {
@ -126,14 +126,15 @@ Required namespace.
using WebSocketSharp.Server; using WebSocketSharp.Server;
`WebSocketServer<T>` class exists in `WebSocketSharp.Server` namespace. `WebSocketServer<T>` class and `WebSocketService` class exist in `WebSocketSharp.Server` namespace.
#### Step 2 #### #### 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, For example, if you want to provide the echo service,
```cs
using System; using System;
using WebSocketSharp; using WebSocketSharp;
using WebSocketSharp.Server; using WebSocketSharp.Server;
@ -145,9 +146,11 @@ For example, if you want to provide the echo service,
Send(e.Data); Send(e.Data);
} }
} }
```
For example, if you want to provide the chat service, For example, if you want to provide the chat service,
```cs
using System; using System;
using WebSocketSharp; using WebSocketSharp;
using WebSocketSharp.Server; 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) protected override void onMessage(object sender, MessageEventArgs e)
{ {
Server.Send(e.Data); Publish(e.Data);
} }
} }
```
#### Step 3 #### #### Step 3 ####
Creating instance of `WebSocketServer<T>` class. Creating a instance of `WebSocketServer<T>` class.
var wssv = new WebSocketServer<Echo>("ws://example.com:4649"); var wssv = new WebSocketServer<Echo>("ws://example.com:4649");
Type of `T` inherits `WebSocketService` class, so you can use a class that was created in **Step 2**. 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<T>` set 80 or 443 to port number automatically. If you set WebSocket url without port number, `WebSocketServer<T>` set **80** or **443** to port number automatically.
So it is necessary to run with root permission. So it is necessary to run with root permission.
$ sudo mono example2.exe $ sudo mono example2.exe

View File

@ -1,5 +1,5 @@
<Properties> <Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release_Ubuntu" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench /> <MonoDevelop.Ide.Workbench />
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore /> <BreakpointStore />

View File

@ -34,12 +34,11 @@ namespace WebSocketSharp.Server
public interface IWebSocketServer public interface IWebSocketServer
{ {
void AddService(WebSocketService service); void AddService(WebSocketService service);
void CloseService(); void CloseServices(CloseStatusCode code, string reason);
void CloseService(CloseStatusCode code, string reason);
void Ping(string data); void Ping(string data);
void Publish(byte[] data);
void Publish(string data);
void RemoveService(WebSocketService service); void RemoveService(WebSocketService service);
void Send(byte[] data);
void Send(string data);
void Start(); void Start();
void Stop(); void Stop();
} }

View File

@ -173,12 +173,12 @@ namespace WebSocketSharp.Server
_services.Add(service); _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) lock (_services.SyncRoot)
{ {
@ -189,6 +189,11 @@ namespace WebSocketSharp.Server
} }
} }
public void Ping()
{
Ping(String.Empty);
}
public void Ping(string data) public void Ping(string data)
{ {
WaitCallback broadcast = (state) => WaitCallback broadcast = (state) =>
@ -204,41 +209,41 @@ namespace WebSocketSharp.Server
ThreadPool.QueueUserWorkItem(broadcast); ThreadPool.QueueUserWorkItem(broadcast);
} }
public void Publish(byte[] data)
{
WaitCallback broadcast = (state) =>
{
lock (_services.SyncRoot)
{
foreach (WebSocketService service in _services)
{
service.Send(data);
}
}
};
ThreadPool.QueueUserWorkItem(broadcast);
}
public void Publish(string data)
{
WaitCallback broadcast = (state) =>
{
lock (_services.SyncRoot)
{
foreach (WebSocketService service in _services)
{
service.Send(data);
}
}
};
ThreadPool.QueueUserWorkItem(broadcast);
}
public void RemoveService(WebSocketService service) public void RemoveService(WebSocketService service)
{ {
_services.Remove(service); _services.Remove(service);
} }
public void Send(byte[] data)
{
WaitCallback broadcast = (state) =>
{
lock (_services.SyncRoot)
{
foreach (WebSocketService service in _services)
{
service.Send(data);
}
}
};
ThreadPool.QueueUserWorkItem(broadcast);
}
public void Send(string data)
{
WaitCallback broadcast = (state) =>
{
lock (_services.SyncRoot)
{
foreach (WebSocketService service in _services)
{
service.Send(data);
}
}
};
ThreadPool.QueueUserWorkItem(broadcast);
}
public void Start() public void Start()
{ {
_tcpListener.Start(); _tcpListener.Start();
@ -248,7 +253,7 @@ namespace WebSocketSharp.Server
public void Stop() public void Stop()
{ {
_tcpListener.Stop(); _tcpListener.Stop();
CloseService(); CloseServices();
} }
#endregion #endregion

View File

@ -33,10 +33,16 @@ namespace WebSocketSharp.Server
{ {
public abstract class WebSocketService public abstract class WebSocketService
{ {
#region Properties #region Private Fields
public IWebSocketServer Server { get; private set; } private IWebSocketServer _server;
public WebSocket Socket { get; private set; } private WebSocket _socket;
#endregion
#region Property
public bool IsBinded { get; private set; }
#endregion #endregion
@ -44,6 +50,7 @@ namespace WebSocketSharp.Server
public WebSocketService() public WebSocketService()
{ {
IsBinded = false;
} }
#endregion #endregion
@ -52,9 +59,14 @@ namespace WebSocketSharp.Server
private void defaultBind() private void defaultBind()
{ {
Socket.OnOpen += (sender, e) => _socket.OnOpen += (sender, e) =>
{ {
Server.AddService(this); _server.AddService(this);
};
_socket.OnClose += (sender, e) =>
{
_server.RemoveService(this);
}; };
} }
@ -76,7 +88,6 @@ namespace WebSocketSharp.Server
protected virtual void onClose(object sender, CloseEventArgs e) protected virtual void onClose(object sender, CloseEventArgs e)
{ {
Server.RemoveService(this);
} }
#endregion #endregion
@ -85,44 +96,71 @@ namespace WebSocketSharp.Server
public void Bind(IWebSocketServer server, WebSocket socket) public void Bind(IWebSocketServer server, WebSocket socket)
{ {
Server = server; _server = server;
Socket = socket; _socket = socket;
defaultBind(); defaultBind();
Socket.OnOpen += onOpen; _socket.OnOpen += onOpen;
Socket.OnMessage += onMessage; _socket.OnMessage += onMessage;
Socket.OnError += onError; _socket.OnError += onError;
Socket.OnClose += onClose; _socket.OnClose += onClose;
IsBinded = true;
}
public void BPing()
{
BPing(String.Empty);
}
public void BPing(string data)
{
if (IsBinded) _server.Ping(data);
} }
public void Close() public void Close()
{ {
Socket.Close(); if (IsBinded) _socket.Close();
} }
public void Close(CloseStatusCode code, string reason) public void Close(CloseStatusCode code, string reason)
{ {
Socket.Close(code, reason); if (IsBinded) _socket.Close(code, reason);
} }
public void Open() public void Open()
{ {
Socket.Connect(); if (IsBinded) _socket.Connect();
}
public void Ping()
{
if (IsBinded) _socket.Ping();
} }
public void Ping(string data) 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) public void Send(byte[] data)
{ {
Socket.Send(data); if (IsBinded) _socket.Send(data);
} }
public void Send(string data) public void Send(string data)
{ {
Socket.Send(data); if (IsBinded) _socket.Send(data);
} }
#endregion #endregion

Binary file not shown.