Fix due to the modified WebSocketServer.cs
This commit is contained in:
parent
2fde8824ec
commit
56c5f81c46
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.
@ -14,7 +14,7 @@ namespace Example2
|
|||||||
wssv.Start();
|
wssv.Start();
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
"WebSocket Server (url: {0})\n listening on address: {1} port: {2}\n",
|
"WebSocket Server (url: {0})\n listening on address: {1} port: {2}\n",
|
||||||
wssv.Url, wssv.Address, wssv.Port);
|
wssv.Uri, 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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -45,7 +45,9 @@ namespace WebSocketSharp.Server {
|
|||||||
#region Fields
|
#region Fields
|
||||||
|
|
||||||
private Thread _acceptClientThread;
|
private Thread _acceptClientThread;
|
||||||
|
private IPAddress _address;
|
||||||
private bool _isSelfHost;
|
private bool _isSelfHost;
|
||||||
|
private int _port;
|
||||||
private Dictionary<string, WebSocketService> _services;
|
private Dictionary<string, WebSocketService> _services;
|
||||||
private TcpListener _tcpListener;
|
private TcpListener _tcpListener;
|
||||||
private Uri _uri;
|
private Uri _uri;
|
||||||
@ -65,33 +67,15 @@ namespace WebSocketSharp.Server {
|
|||||||
#region Public Constructors
|
#region Public Constructors
|
||||||
|
|
||||||
public WebSocketServer(string url)
|
public WebSocketServer(string url)
|
||||||
|
: this()
|
||||||
{
|
{
|
||||||
_uri = new Uri(url);
|
var uri = new Uri(url);
|
||||||
if (!isValidScheme(_uri))
|
|
||||||
{
|
|
||||||
var msg = "Unsupported WebSocket URI scheme: " + _uri.Scheme;
|
|
||||||
throw new ArgumentException(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
var host = _uri.DnsSafeHost;
|
string msg;
|
||||||
var ips = Dns.GetHostAddresses(host);
|
if (!isValidUri(uri, out msg))
|
||||||
if (ips.Length == 0)
|
throw new ArgumentException(msg, "url");
|
||||||
{
|
|
||||||
var msg = "Invalid WebSocket URI host: " + host;
|
|
||||||
throw new ArgumentException(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
var scheme = _uri.Scheme;
|
_tcpListener = new TcpListener(_address, _port);
|
||||||
var port = _uri.Port;
|
|
||||||
if (port <= 0)
|
|
||||||
{
|
|
||||||
port = 80;
|
|
||||||
if (scheme == "wss")
|
|
||||||
port = 443;
|
|
||||||
}
|
|
||||||
|
|
||||||
_tcpListener = new TcpListener(ips[0], port);
|
|
||||||
_services = new Dictionary<string, WebSocketService>();
|
|
||||||
_isSelfHost = true;
|
_isSelfHost = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,15 +84,21 @@ namespace WebSocketSharp.Server {
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public WebSocketServer(int port, string absPath)
|
public WebSocketServer(int port, string path)
|
||||||
|
: this()
|
||||||
{
|
{
|
||||||
_uri = new Uri(absPath, UriKind.Relative);
|
var uri = path.ToUri();
|
||||||
|
if (uri.IsAbsoluteUri)
|
||||||
|
{
|
||||||
|
var msg = "Not absolute path: " + path;
|
||||||
|
throw new ArgumentException(msg, "path");
|
||||||
|
}
|
||||||
|
|
||||||
if (port <= 0)
|
_uri = uri;
|
||||||
port = 80;
|
_address = IPAddress.Any;
|
||||||
|
_port = port <= 0 ? 80 : port;
|
||||||
|
|
||||||
_tcpListener = new TcpListener(IPAddress.Any, port);
|
_tcpListener = new TcpListener(_address, _port);
|
||||||
_services = new Dictionary<string, WebSocketService>();
|
|
||||||
_isSelfHost = true;
|
_isSelfHost = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,12 +108,7 @@ namespace WebSocketSharp.Server {
|
|||||||
|
|
||||||
public IPAddress Address
|
public IPAddress Address
|
||||||
{
|
{
|
||||||
get { return Endpoint.Address; }
|
get { return _address; }
|
||||||
}
|
|
||||||
|
|
||||||
public IPEndPoint Endpoint
|
|
||||||
{
|
|
||||||
get { return (IPEndPoint)_tcpListener.LocalEndpoint; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsSelfHost {
|
public bool IsSelfHost {
|
||||||
@ -132,12 +117,12 @@ namespace WebSocketSharp.Server {
|
|||||||
|
|
||||||
public int Port
|
public int Port
|
||||||
{
|
{
|
||||||
get { return Endpoint.Port; }
|
get { return _port; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Url
|
public Uri Uri
|
||||||
{
|
{
|
||||||
get { return _uri.ToString(); }
|
get { return _uri; }
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -181,13 +166,42 @@ namespace WebSocketSharp.Server {
|
|||||||
OnError.Emit(this, new ErrorEventArgs(message));
|
OnError.Emit(this, new ErrorEventArgs(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool isValidScheme(Uri uri)
|
private bool isValidUri(Uri uri, out string message)
|
||||||
{
|
{
|
||||||
var scheme = uri.Scheme;
|
var scheme = uri.Scheme;
|
||||||
if (scheme == "ws" || scheme == "wss")
|
var port = uri.Port;
|
||||||
return true;
|
var host = uri.DnsSafeHost;
|
||||||
|
var ips = Dns.GetHostAddresses(host);
|
||||||
|
|
||||||
return false;
|
if (scheme != "ws" && scheme != "wss")
|
||||||
|
{
|
||||||
|
message = "Unsupported WebSocket URI scheme: " + scheme;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((scheme == "wss" && port != 443) ||
|
||||||
|
(scheme != "wss" && port == 443))
|
||||||
|
{
|
||||||
|
message = String.Format(
|
||||||
|
"Invalid pair of WebSocket URI scheme and port: {0}, {1}", scheme, port);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ips.Length == 0)
|
||||||
|
{
|
||||||
|
message = "Invalid WebSocket URI host: " + host;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (port <= 0)
|
||||||
|
port = scheme == "ws" ? 80 : 443;
|
||||||
|
|
||||||
|
_uri = uri;
|
||||||
|
_address = ips[0];
|
||||||
|
_port = port;
|
||||||
|
|
||||||
|
message = String.Empty;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startAcceptClientThread()
|
private void startAcceptClientThread()
|
||||||
@ -199,7 +213,7 @@ namespace WebSocketSharp.Server {
|
|||||||
|
|
||||||
private void startService(TcpClient client)
|
private void startService(TcpClient client)
|
||||||
{
|
{
|
||||||
WaitCallback startSv = (state) =>
|
WaitCallback startServiceCb = (state) =>
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
var socket = new WebSocket(_uri, client);
|
var socket = new WebSocket(_uri, client);
|
||||||
@ -210,7 +224,7 @@ namespace WebSocketSharp.Server {
|
|||||||
error(ex.Message);
|
error(ex.Message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ThreadPool.QueueUserWorkItem(startSv);
|
ThreadPool.QueueUserWorkItem(startServiceCb);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user