c# - websocketsharp opens multiple concurrent connections in same client
问题描述
I have a client/server application, that works by sending/receiving commands, and if the connections is not open, it shall attempt to reconnect and send the command.
The problem is, the client opens multiple connections and send the same command over all of them, so the server ends up receiving 4, 5, 10+ same commands from the same client, which causes to send a response x10 times to the client.
Also, if the connection drops/socket closes, it should attempt to reconnect, but, instead it will try to open infinite amounts of connections concurrent.
This is a sample of connection and single command.
public void Start(int attempt = 0)
{
try
{
if (!ws.IsAlive)
{
ws.OnMessage += Ws_OnMessage;
DisableLog.Disable(ws.Log);
ws.Connect();
if ((!ws.IsAlive) && attempt <= 10) { AddLog(1, $"{attempt}/10 No connection to remote server could be made."); ws.Close(); Thread.Sleep(5000); Start(++attempt); }
else if ((!ws.IsAlive) && attempt > 10) { AddLog(1, "Unable to connect to the server. Program will restart."); ws.Close(); Program.restart(); }
ws.OnClose += Ws_OnClose;
}
}
catch
{
}
}
private void Ws_OnClose(object sender, CloseEventArgs e)
{
try
{
if (!e.WasClean)
{
ws.Close();
Start();
}
} catch { }
}
public bool Set(int x = 0)
{
try
{
if (x > 5) { AddLog(1, "Socket error"); return false; }
else
{
if (ws.IsAlive) { ws.Send($"{command}"); }
else { Start(); Set(++x); }
string data = checkResponse("set");
string[] decode = data.Split(',');
if (decode[0] != "error") { return true; } else { return false; }
}
} catch { return false; }
}
static void Main(string[] args)
{
Start();
if (Set()) Console.WriteLine("command successful");
}
In those scenarios:
a) the socket connection is lost, it will attempt to reconnect max 10 times, but, this is instead the output:
0/10 No connection to remote server could be made.
1/10 No connection to remote server could be made.
0/10 No connection to remote server could be made.
2/10 No connection to remote server could be made.
1/10 No connection to remote server could be made.
0/10 No connection to remote server could be made.
3/10 No connection to remote server could be made.
1/10 No connection to remote server could be made.
4/10 No connection to remote server could be made.
2/10 No connection to remote server could be made.
0/10 No connection to remote server could be made.
3/10 No connection to remote server could be made.
-> it goes on infinite amount of times multiple/concurrent
b) the command is being tried to send, but it fails due to connection, then it will attempt to reconnect and send again, but instead it opens multiple concurrent connections instead of a single one, and thus the output is this:
Socket Error
command successful
command successful
command successful
command successful
command successful
Received response from server 1
Received response from server 1
Received response from server 1
Received response from server 1
Received response from server 1
-> this should happen only once, but it's being spammed multiple times
How do I stop this behavior?
解决方案
推荐阅读
- amazon-web-services - amazon s3 预签名的 url 应该使用新的访问密钥辞职
- javascript - 将数组传递给 JavaScript 显示为 Object
- java - jhipster为什么不更新
- html - 使用 css 反映 svg
- android - Firebase 验证 Id 令牌,但客户端尚未准备好,所以我不知道来自客户端的数据包含
- css-selectors - DomCrawler 第二个等孩子
- node.js - 为什么我的节点 js express 应用程序不能保持活动状态?
- java - Java:使所有字段成为最终字段或可变字段?
- python - 在 Paramiko 的 SFTP 路径中使用环境变量
- python-3.x - 我想创建一个游戏