我正在开发一个针对Rhino的Grasshopper插件,它使用.NET框架和C#,重新连接到socket.io 1.4.5服务器时遇到问题。我使用SocketIoClientDotNet 0.9.13连接到服务器,如果我只是想连接/断开连接到服务器,它工作正常,但是在我收到使用事件拍摄的消息后重新连接到服务器时出现问题时间重新连接。
取决于我在连接期间收到的消息数量,在我关闭并建立新连接后,新连接需要超过一分钟(如果收到大量消息,则有时超过10分钟)
有人可以说出出了什么问题吗?
代码编写如下:
Quobject.SocketIoClientDotNet.Client.Socket socket;
private System.Threading.ManualResetEvent manualResetEvent = null;
private bool currentState = false;
public object output = null;
public bool connected = false;
private Quobject.SocketIoClientDotNet.Client.IO.Options CreateOptions()
{
Quobject.SocketIoClientDotNet.Client.IO.Options op = new Quobject.SocketIoClientDotNet.Client.IO.Options();
op.AutoConnect = true;
op.Reconnection = true;
op.ReconnectionAttempts = 5;
op.ReconnectionDelay = 5;
op.Timeout = 20;
op.Secure = true;
op.ForceNew = true;
op.Multiplex = true;
return op;
}
private void ConnectToSocket(bool b, string address){
if(currentState != b){
if(b && !connected){
manualResetEvent = new System.Threading.ManualResetEvent(false);
var options = CreateOptions();
socket = IO.Socket(address, options);
socket.On(Quobject.SocketIoClientDotNet.Client.Socket.EVENT_CONNECT, () =>
{
connected = true;
Print("connected");
manualResetEvent.Set();
});
socket.On("slider_message", (data) =>
{
if(data != null){
var jobject = data as JToken;
try{
var sliderValue = jobject.Value<string>("slider");
output = sliderValue;
}catch{
}
}
manualResetEvent.Set();
});
socket.On(Quobject.SocketIoClientDotNet.Client.Socket.EVENT_DISCONNECT, () =>
{
connected = false;
Print("disconnected");
manualResetEvent.Set();
});
Print("connecting...");
manualResetEvent.WaitOne();
}else{
if(socket != null & connected){
manualResetEvent = new System.Threading.ManualResetEvent(false);
Print("disconnecting...");
socket.Close();
manualResetEvent.WaitOne();
socket = null;
}
}
}
currentState = b;
}