小编Ads*_*ter的帖子

如何强制断开SignalR中的客户端

我正在测试SignalR(通过nuget 0.4.0)并且找不到任何方法让服务器强行断开客户端.我想我错过了一些明显的东西.

我的测试代码在下面,我已经在几乎每个地方和组合中尝试了Close()和Timeout(),我认为没有成功.客户端继续接收脉冲的消息,虽然我总是似乎来自第一4-5秒内得到2重新连接return Connection.Close()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));
    } …
Run Code Online (Sandbox Code Playgroud)

c# signalr

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×1

signalr ×1