2012-02-21 51 views
5

我正在测试SignalR(0.4.0 via nuget)并且找不到任何方式让服务器强制断开客户端连接。我想我错过了一些明显的东西。如何强制断开SignalR中的客户端

我的测试代码在下面,我已经在几乎每个地方都尝试过Close()和Timeout(),并且我可以想象它们没有成功。客户端继续接收脉冲的消息,虽然我总是出现在来自return Connection.Close()第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(); 
    } 
} 

客户:

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-223ef 96fe96c连接
你好
脉冲
脉冲
d7615b15-f80c-4bc5-b37b-223ef96fe96c重新连接
脉冲
脉冲
d7615b15-f80c-4bc5-b37b-223ef96fe96c重新连接
脉冲
脉冲
脉冲
脉冲
脉冲
脉冲
...

回答

1

发送一个特定的字符串,客户端强制断开:

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

    Console.WriteLine(data); 
} 
+0

谢谢,这将工作,但我很惊讶没有什么内置的来源。 Close()方法建议它向客户端发送消息以断开连接,但我无法实际实现。 – Adster 2012-02-22 11:26:41

+1

这是.net客户端中的一个错误。你为什么试图强迫连接关闭? – davidfowl 2012-02-22 21:46:54

+0

@dfowler我正在构建一个代理应用程序,位于现有基于套接字的服务器和移动Web客户端之间。如果套接字在服务器上关闭,我想将该断开连接转发给客户端。我现在要和nmat的建议一起去,并且当我有一个真正的JS客户端进行测试时再次测试close()。干杯 – Adster 2012-02-23 10:19:41