我的应用程序是使用SignalR进行通信的服务/客户端。我正在尝试实现方案,其中SignalR服务器正常切换到另一个端口。以下是服务器创建或切换的方式:
public void StartListen(int port)
{
StopListen();
var url = string.Format("http://localhost:{0}/test1", port);
webApp = WebApp.Start(url, app =>
{
app.MapSignalR<Endpoint>("/endpoint");
});
Log("Listening on {0}", url);
}
public void StopListen()
{
if (webApp != null)
{
webApp.Dispose();
webApp = null;
}
}
public void Pong()
{
var context = GlobalHost.ConnectionManager.GetConnectionContext<Endpoint>();
context.Connection.Broadcast("pong");
}
端点是:
public class Endpoint : PersistentConnection
{
protected override Task OnReceived(IRequest r, string connectionId, string data)
{
return Task.Run(() => Host.Log("Got: {0}", data));
}
}
我还有一个客户端对象可以切换到新端口的连接,如下所示:
Microsoft.AspNet.SignalR.Client.Connection connection;
public void StartConnection(int port)
{
StopConnection();
var url = string.Format("http://localhost:{0}/test1/endpoint", port);
connection = new Microsoft.AspNet.SignalR.Client.Connection(url);
connection.Start().ContinueWith(task => Log("Connected"));
}
public void StopConnection()
{
if (connection != null)
{
connection.Stop();
connection.Dispose();
connection = null;
Log("stopped");
}
}
private void ConnectionReceivedHandler(string obj)
{
Log("Got: {0}", obj);
}
public void Ping()
{
connection.Send("ping");
}
这是正确的实施吗?我问的原因是以下一系列行动会导致恶意:
但是,以下序列工作正常:
我有一种怀疑,我没有做一些我为了让shenanigan工作而被要求做的事情