Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Signal中强制断开客户端连接_C#_Signalr - Fatal编程技术网

C# 如何在Signal中强制断开客户端连接

C# 如何在Signal中强制断开客户端连接,c#,signalr,C#,Signalr,我正在测试signar(通过nuget测试0.4.0),但找不到任何方法让服务器强制断开客户端的连接。我想我错过了一些明显的东西 下面是我的测试代码,我在几乎所有我能想到的地方和组合尝试了Close()和Timeout(),但都没有成功。客户端继续接收脉冲消息,尽管我总是在头4-5秒内重新连接2次,这似乎来自返回连接。在OnReceivedAsync()中关闭() 服务器: internal class SignalRServer { private Server server;

我正在测试signar(通过nuget测试0.4.0),但找不到任何方法让服务器强制断开客户端的连接。我想我错过了一些明显的东西

下面是我的测试代码,我在几乎所有我能想到的地方和组合尝试了Close()和Timeout(),但都没有成功。客户端继续接收脉冲消息,尽管我总是在头4-5秒内重新连接2次,这似乎来自
返回连接。在
OnReceivedAsync()中关闭()

服务器:

internal class SignalRServer
{
    private Server server;

    public SignalRServer()
    {
        server = new Server("http://localhost:13170/");
        server.MapConnection<EchoConnection>("/echo");
        server.Start();
        Timer timer = new Timer(1000);
        timer.Elapsed += OnTimer;
        timer.Enabled = true;
    }

    void OnTimer(object sender, ElapsedEventArgs e)
    {
        IConnectionManager manager = server.DependencyResolver.GetService(typeof(IConnectionManager)) as IConnectionManager;
        IConnection connection = manager.GetConnection<EchoConnection>();
        connection.Broadcast("pulse");
        connection.Close();
        connection.Timeout();
    }
}

internal class EchoConnection : PersistentConnection
{
    protected override Task OnConnectedAsync(IRequest request, IEnumerable<string> groups, string connectionId)
    {
        Connection.Timeout();
        Connection.Close();
        return Connection.Broadcast(String.Format("{0} connection", connectionId));
    }

    protected override Task OnReconnectedAsync(IRequest request, IEnumerable<string> groups, string connectionId)
    {
        return Connection.Broadcast(String.Format("{0} reconnection", connectionId));
    }

    protected override Task OnReceivedAsync(string connectionId, string data)
    {
        Console.WriteLine(data);
        Connection.Close();
        Connection.Timeout();
        Connection.Broadcast(data);
        return Connection.Close();
    }
}
客户机输出示例:

d7615b15-f80c-4bc5-b37b-223ef96fe96c连接
你好
脉搏
脉搏
d7615b15-f80c-4bc5-b37b-223ef96fe96c重连
脉搏
脉搏
d7615b15-f80c-4bc5-b37b-223ef96fe96c重连
脉搏
脉搏
脉搏
脉搏
脉搏
脉搏


向客户端发送特定字符串以强制断开连接:

void OnReceive(string data)
{
    if(data.Equals("exit"))
    {
        connection.Stop();
        return;
    }

    Console.WriteLine(data);
}
在.Net内核中使用

Context.Abort();

谢谢,那会有用的,但我很惊讶里面没有内置任何东西。Close()方法的源代码建议它向客户端发送一条消息以断开连接,但我无法让它实际这样做。这是.net客户端中的一个错误。为什么要强制关闭连接?@dfowler我正在构建一个代理应用程序,位于现有基于套接字的服务器和移动web客户端之间。如果服务器上的套接字关闭,我想将该断开连接中继到客户端。现在我将按照nmat的建议,在有一个实际的JS客户机进行测试时再次测试close()。干杯
Context.Abort();