我使用Web API和Microsoft.WebSockets包实现了Web套接字服务器。
public class MyWSHandler : Microsoft.Web.WebSockets.WebSocketHandler
{
public string _username;
public MyWSHandler(string username)
{
_username = username;
}
public override void OnOpen()
{
System.Diagnostics.Debug.Write("Socket Opened!");
if (!Module._clients.Any(x => ((MyWSHandler)x)._username == this._username)) {
Module._clients.Add(this);
}
Module._clients.Broadcast("User "+this._username+" has connected!");
}
}
我可以从JavaScript客户端代码连接到服务器。然后,我尝试使用带有C#的System.Net.WebSockets.WebSocketClient类从桌面应用程序连接到服务器。
try
{
var websocket = new ClientWebSocket();
await websocket.ConnectAsync(new Uri(@"ws://localhost:5587/api/ws?username=admin"), CancellationToken.None);
await Task.WhenAll(Receive(websocket), Send(websocket));
}
catch(Exception ex){
Console.WriteLine("Exception: {0}", ex);
}
但是,远程服务器会抛出错误(404)。我错过了什么吗?