首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SignalR中强行断开客户端

如何在SignalR中强行断开客户端
EN

Stack Overflow用户
提问于 2012-02-21 17:23:44
回答 2查看 16.7K关注 0票数 8

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

我的测试代码如下所示,我几乎在我能想到的每一个地方和组合中都尝试了Close()和Timeout(),但都没有成功。客户端继续接收脉冲消息,尽管我总是在最初的4-5秒内获得两个重新连接,这些连接似乎来自于return Connection.Close() in OnReceivedAsync()

服务器:

代码语言:javascript
复制
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();
    }
}

客户端:

代码语言:javascript
复制
internal class SignalRClient
{
    private readonly Connection connection;

    public SignalRClient()
    {
        connection = new Connection("http://localhost:13170/echo");
        connection.Received += OnReceive;
        connection.Closed += OnClosed;
        connection
            .Start()
            .ContinueWith(t =>
            {
                if (!t.IsFaulted)
                    connection.Send("Hello");
                else
                    Console.WriteLine(t.Exception);
            });


        Console.WriteLine(connection.ConnectionId);
    }

    void OnClosed()
    {
        // never called
        connection.Stop();
    }

    void OnReceive(string data)
    {
        Console.WriteLine(data);
    }
}

示例客户端输出:

d7615b15-f80c-4bc5-b37b-223 ef96fe96c连接

你好

脉搏

脉搏

d7615b15-f80c-4bc5-b37b-223ef96fe96c重连接

脉搏

脉搏

d7615b15-f80c-4bc5-b37b-223ef96fe96c重连接

脉搏

脉搏

脉搏

脉搏

脉搏

脉搏

.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-21 23:48:07

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

代码语言:javascript
复制
void OnReceive(string data)
{
    if(data.Equals("exit"))
    {
        connection.Stop();
        return;
    }

    Console.WriteLine(data);
}
票数 3
EN

Stack Overflow用户

发布于 2021-02-10 15:21:57

在.Net核心使用中

代码语言:javascript
复制
Context.Abort();

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.hubcallercontext.abort

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9382111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档