[英]C# & JS WebSocket Some users can't connect to server
我現在遇到了一個很大的問題:我有一台服務器,用戶可以在其中使用Alchemy庫連接和發送和接收用C#編寫的消息,但是現在我收到一些用戶的報告,說他們無法連接到Websocket服務器。 在Serverlog中,它們自動斷開連接而不是連接。
這是C#代碼:
internal static bool Init(int bindPort, int maxConnections)
{
_Port = bindPort;
_maxConnections = maxConnections;
_activeConnections = new HashSet<int>();
try
{
var aServer = new WebSocketServer(_Port, IPAddress.Any)
{
OnReceive = OnReceive,
OnSend = OnSend,
OnConnected = OnConnect,
OnDisconnect = OnDisconnect,
TimeOut = new TimeSpan(0, 5, 0)
};
aServer.Start();
Console.WriteLine(" [] WebSockets port: " + _Port + ".");
return true;
}
catch
{
Console.WriteLine(" WebSocket konnte nicht geladen werden");
return false;
}
}
public static void SendAll(string data)
{
foreach(UserContext Kubbo in OnlineUsers.Keys)
{
Kubbo.Send(data);
}
}
private static void OnConnect(UserContext context)
{
try
{
int connectionID = 0;
for (int i = 1; i < _maxConnections; i++)
{
if (_activeConnections.Contains(i) == false)
{
connectionID = i;
break;
}
}
if (connectionID > 0)
{
Console.WriteLine("[" + connectionID + "] Verbindung " + context.ClientAddress);
context.Send("A");
_activeConnections.Add(connectionID);
_acceptedConnections++;
var me = new User { Context = context };
OnlineUsers.Add(context, context.ClientAddress.ToString());
}
}
catch
{
}
}
internal static void OnDisconnect(UserContext context)
{
try
{
OnlineUsers.Remove((UserContext)context);
Console.WriteLine("[] Verbindung wurde geschloßen");
}
catch
{
Console.WriteLine("{}");
}
}
Javascript代碼(使用在JavaScript中實現的常規Socket API確實很簡單):
var ws = new WebSocket("ws://localhost:38819/");
ws.onopen = function()
{
ws.send("A");
alert("Connection sucessfully opened");
};
ws.onclose = function()
{
alert("Connection is closed...");
};
服務器在什么操作系統上運行? 例如,Windows 7 Ultimate默認為20個並發連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.