Fix due to the added ID property to WebSocketService
This commit is contained in:
parent
0bea2dd623
commit
5f4f7485aa
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.
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.
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.
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.
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -27,6 +27,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using WebSocketSharp.Frame;
|
||||
|
||||
namespace WebSocketSharp.Server
|
||||
@ -35,13 +36,14 @@ namespace WebSocketSharp.Server
|
||||
{
|
||||
WsServerState State { get; }
|
||||
|
||||
void AddService(WebSocketService service);
|
||||
void CloseServices(CloseStatusCode code, string reason);
|
||||
void Ping(string data);
|
||||
void Publish(byte[] data);
|
||||
void Publish(string data);
|
||||
void RemoveService(WebSocketService service);
|
||||
void AddService(string id, WebSocketService service);
|
||||
Dictionary<string, bool> PingAround(string data);
|
||||
void Publish<TData>(TData data);
|
||||
void RemoveService(string id);
|
||||
void SendTo<TData>(string id, TData data);
|
||||
void SendTo<TData>(IEnumerable<string> group, TData data);
|
||||
void Start();
|
||||
void Stop();
|
||||
void StopServices(CloseStatusCode code, string reason);
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
@ -44,7 +45,8 @@ namespace WebSocketSharp.Server
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private SynchronizedCollection<WebSocketService> _services;
|
||||
private object _forServices;
|
||||
private Dictionary<string, WebSocketService> _services;
|
||||
private WsServerState _state;
|
||||
private TcpListener _tcpListener;
|
||||
private Uri _uri;
|
||||
@ -91,9 +93,11 @@ namespace WebSocketSharp.Server
|
||||
public WebSocketServer(string url)
|
||||
{
|
||||
_uri = new Uri(url);
|
||||
|
||||
if (!isValidScheme(_uri))
|
||||
{
|
||||
throw new ArgumentException("Unsupported WebSocket URI scheme: " + _uri.Scheme);
|
||||
var msg = "Unsupported WebSocket URI scheme: " + _uri.Scheme;
|
||||
throw new ArgumentException(msg);
|
||||
}
|
||||
|
||||
string scheme = _uri.Scheme;
|
||||
@ -112,7 +116,8 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
|
||||
_tcpListener = new TcpListener(IPAddress.Any, port);
|
||||
_services = new SynchronizedCollection<WebSocketService>();
|
||||
_forServices = new object();
|
||||
_services = new Dictionary<string, WebSocketService>();
|
||||
_state = WsServerState.READY;
|
||||
}
|
||||
|
||||
@ -132,10 +137,10 @@ namespace WebSocketSharp.Server
|
||||
try
|
||||
{
|
||||
TcpClient client = listener.EndAcceptTcpClient(ar);
|
||||
WebSocket socket = new WebSocket(_uri.ToString(), client);
|
||||
WebSocket socket = new WebSocket(_uri, client);
|
||||
T service = new T();
|
||||
service.Bind(this, socket);
|
||||
service.Open();
|
||||
service.Start();
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
@ -175,80 +180,99 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void AddService(WebSocketService service)
|
||||
public void AddService(string id, WebSocketService service)
|
||||
{
|
||||
_services.Add(service);
|
||||
}
|
||||
|
||||
public void CloseServices()
|
||||
lock (_forServices)
|
||||
{
|
||||
CloseServices(CloseStatusCode.NORMAL, String.Empty);
|
||||
}
|
||||
|
||||
public void CloseServices(CloseStatusCode code, string reason)
|
||||
{
|
||||
lock (_services.SyncRoot)
|
||||
{
|
||||
foreach (WebSocketService service in _services)
|
||||
{
|
||||
service.Close(code, reason);
|
||||
}
|
||||
_services.Add(id, service);
|
||||
}
|
||||
}
|
||||
|
||||
public void Ping()
|
||||
public Dictionary<string, bool> PingAround()
|
||||
{
|
||||
Ping(String.Empty);
|
||||
return PingAround(String.Empty);
|
||||
}
|
||||
|
||||
public void Ping(string data)
|
||||
public Dictionary<string, bool> PingAround(string data)
|
||||
{
|
||||
var result = new Dictionary<string, bool>();
|
||||
|
||||
lock (_forServices)
|
||||
{
|
||||
foreach (WebSocketService service in _services.Values)
|
||||
{
|
||||
result.Add(service.ID, service.Ping(data));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Publish<TData>(TData data)
|
||||
{
|
||||
WaitCallback broadcast = (state) =>
|
||||
{
|
||||
lock (_services.SyncRoot)
|
||||
lock (_forServices)
|
||||
{
|
||||
foreach (WebSocketService service in _services)
|
||||
{
|
||||
service.Ping(data);
|
||||
}
|
||||
SendTo(_services.Keys, data);
|
||||
}
|
||||
};
|
||||
ThreadPool.QueueUserWorkItem(broadcast);
|
||||
}
|
||||
|
||||
public void Publish(byte[] data)
|
||||
public void RemoveService(string id)
|
||||
{
|
||||
WaitCallback broadcast = (state) =>
|
||||
lock (_forServices)
|
||||
{
|
||||
lock (_services.SyncRoot)
|
||||
{
|
||||
foreach (WebSocketService service in _services)
|
||||
{
|
||||
service.Send(data);
|
||||
_services.Remove(id);
|
||||
}
|
||||
}
|
||||
};
|
||||
ThreadPool.QueueUserWorkItem(broadcast);
|
||||
}
|
||||
|
||||
public void Publish(string data)
|
||||
public void SendTo<TData>(string id, TData data)
|
||||
{
|
||||
WaitCallback broadcast = (state) =>
|
||||
if (typeof(TData) != typeof(string) &&
|
||||
typeof(TData) != typeof(byte[]))
|
||||
{
|
||||
lock (_services.SyncRoot)
|
||||
{
|
||||
foreach (WebSocketService service in _services)
|
||||
{
|
||||
service.Send(data);
|
||||
}
|
||||
}
|
||||
};
|
||||
ThreadPool.QueueUserWorkItem(broadcast);
|
||||
var msg = "Type of data must be string or byte[].";
|
||||
throw new ArgumentException(msg);
|
||||
}
|
||||
|
||||
public void RemoveService(WebSocketService service)
|
||||
lock (_forServices)
|
||||
{
|
||||
_services.Remove(service);
|
||||
WebSocketService service;
|
||||
|
||||
if (_services.TryGetValue(id, out service))
|
||||
{
|
||||
if (typeof(TData) == typeof(string))
|
||||
{
|
||||
string data_ = (string)(object)data;
|
||||
service.Send(data_);
|
||||
}
|
||||
else if (typeof(TData) == typeof(byte[]))
|
||||
{
|
||||
byte[] data_ = (byte[])(object)data;
|
||||
service.Send(data_);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SendTo<TData>(IEnumerable<string> group, TData data)
|
||||
{
|
||||
if (typeof(TData) != typeof(string) &&
|
||||
typeof(TData) != typeof(byte[]))
|
||||
{
|
||||
var msg = "Type of data must be string or byte[].";
|
||||
throw new ArgumentException(msg);
|
||||
}
|
||||
|
||||
lock (_forServices)
|
||||
{
|
||||
foreach (string id in group)
|
||||
{
|
||||
SendTo(id, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Start()
|
||||
@ -263,11 +287,27 @@ namespace WebSocketSharp.Server
|
||||
_state = WsServerState.SHUTDOWN;
|
||||
|
||||
_tcpListener.Stop();
|
||||
CloseServices();
|
||||
StopServices();
|
||||
|
||||
_state = WsServerState.STOP;
|
||||
}
|
||||
|
||||
public void StopServices()
|
||||
{
|
||||
StopServices(CloseStatusCode.NORMAL, String.Empty);
|
||||
}
|
||||
|
||||
public void StopServices(CloseStatusCode code, string reason)
|
||||
{
|
||||
lock (_forServices)
|
||||
{
|
||||
foreach (WebSocketService service in _services.Values)
|
||||
{
|
||||
service.Stop(code, reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using WebSocketSharp.Frame;
|
||||
|
||||
namespace WebSocketSharp.Server
|
||||
@ -40,8 +41,9 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#endregion
|
||||
|
||||
#region Property
|
||||
#region Properties
|
||||
|
||||
public string ID { get; private set; }
|
||||
public bool IsBound { get; private set; }
|
||||
|
||||
#endregion
|
||||
@ -50,6 +52,7 @@ namespace WebSocketSharp.Server
|
||||
|
||||
public WebSocketService()
|
||||
{
|
||||
ID = String.Empty;
|
||||
IsBound = false;
|
||||
}
|
||||
|
||||
@ -57,18 +60,24 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#region Private Method
|
||||
|
||||
private string getNewID()
|
||||
{
|
||||
return Guid.NewGuid().ToString("N");
|
||||
}
|
||||
|
||||
private void defaultBind()
|
||||
{
|
||||
_socket.OnOpen += (sender, e) =>
|
||||
{
|
||||
_server.AddService(this);
|
||||
ID = getNewID();
|
||||
_server.AddService(ID, this);
|
||||
};
|
||||
|
||||
_socket.OnClose += (sender, e) =>
|
||||
{
|
||||
if (_server.State == WsServerState.START)
|
||||
{
|
||||
_server.RemoveService(this);
|
||||
_server.RemoveService(ID);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -111,47 +120,30 @@ namespace WebSocketSharp.Server
|
||||
IsBound = true;
|
||||
}
|
||||
|
||||
public void BPing()
|
||||
public Dictionary<string, bool> PingAround()
|
||||
{
|
||||
BPing(String.Empty);
|
||||
return PingAround(String.Empty);
|
||||
}
|
||||
|
||||
public void BPing(string data)
|
||||
public Dictionary<string, bool> PingAround(string data)
|
||||
{
|
||||
if (IsBound) _server.Ping(data);
|
||||
if (IsBound) return _server.PingAround(data);
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
public bool Ping()
|
||||
{
|
||||
if (IsBound) _socket.Close();
|
||||
if (IsBound) return _socket.Ping();
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Close(CloseStatusCode code, string reason)
|
||||
public bool Ping(string data)
|
||||
{
|
||||
if (IsBound) _socket.Close(code, reason);
|
||||
if (IsBound) return _socket.Ping(data);
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Open()
|
||||
{
|
||||
if (IsBound) _socket.Connect();
|
||||
}
|
||||
|
||||
public void Ping()
|
||||
{
|
||||
if (IsBound) _socket.Ping();
|
||||
}
|
||||
|
||||
public void Ping(string data)
|
||||
{
|
||||
if (IsBound) _socket.Ping(data);
|
||||
}
|
||||
|
||||
public void Publish(byte[] data)
|
||||
{
|
||||
if (IsBound) _server.Publish(data);
|
||||
}
|
||||
|
||||
public void Publish(string data)
|
||||
public void Publish<TData>(TData data)
|
||||
{
|
||||
if (IsBound) _server.Publish(data);
|
||||
}
|
||||
@ -166,6 +158,31 @@ namespace WebSocketSharp.Server
|
||||
if (IsBound) _socket.Send(data);
|
||||
}
|
||||
|
||||
public void SendTo<TData>(string id, TData data)
|
||||
{
|
||||
if (IsBound) _server.SendTo(id, data);
|
||||
}
|
||||
|
||||
public void SendTo<TData>(IEnumerable<string> group, TData data)
|
||||
{
|
||||
if (IsBound) _server.SendTo(group, data);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (IsBound) _socket.Connect();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (IsBound) _socket.Close();
|
||||
}
|
||||
|
||||
public void Stop(CloseStatusCode code, string reason)
|
||||
{
|
||||
if (IsBound) _socket.Close(code, reason);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -61,9 +61,9 @@ namespace WebSocketSharp
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private AutoResetEvent _autoEvent;
|
||||
private string _base64key;
|
||||
private string _binaryType;
|
||||
private AutoResetEvent _exitedMessageLoop;
|
||||
private string _extensions;
|
||||
private Object _forClose;
|
||||
private Object _forSend;
|
||||
@ -190,15 +190,10 @@ namespace WebSocketSharp
|
||||
|
||||
#region Internal Constructors
|
||||
|
||||
internal WebSocket(string url, TcpClient tcpClient)
|
||||
internal WebSocket(Uri uri, TcpClient tcpClient)
|
||||
: this()
|
||||
{
|
||||
_uri = new Uri(url);
|
||||
if (!isValidScheme(_uri))
|
||||
{
|
||||
throw new ArgumentException("Unsupported WebSocket URI scheme: " + _uri.Scheme);
|
||||
}
|
||||
|
||||
_uri = uri;
|
||||
_tcpClient = tcpClient;
|
||||
_isClient = false;
|
||||
}
|
||||
@ -211,9 +206,11 @@ namespace WebSocketSharp
|
||||
: this()
|
||||
{
|
||||
_uri = new Uri(url);
|
||||
|
||||
if (!isValidScheme(_uri))
|
||||
{
|
||||
throw new ArgumentException("Unsupported WebSocket URI scheme: " + _uri.Scheme);
|
||||
var msg = "Unsupported WebSocket URI scheme: " + _uri.Scheme;
|
||||
throw new ArgumentException(msg);
|
||||
}
|
||||
|
||||
_protocols = protocols.ToString(", ");
|
||||
@ -346,11 +343,11 @@ namespace WebSocketSharp
|
||||
{
|
||||
if (_isClient)
|
||||
{
|
||||
_msgThread.Join(5000);
|
||||
_msgThread.Join(5 * 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
_autoEvent.WaitOne();
|
||||
_exitedMessageLoop.WaitOne(5 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@ -788,7 +785,7 @@ namespace WebSocketSharp
|
||||
}
|
||||
else
|
||||
{
|
||||
_autoEvent.Set();
|
||||
_exitedMessageLoop.Set();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1118,7 +1115,7 @@ namespace WebSocketSharp
|
||||
}
|
||||
else
|
||||
{
|
||||
_autoEvent = new AutoResetEvent(false);
|
||||
_exitedMessageLoop = new AutoResetEvent(false);
|
||||
Action messageInvoker = () =>
|
||||
{
|
||||
if (_readyState == WsState.OPEN)
|
||||
|
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.
Loading…
Reference in New Issue
Block a user