I am testing SignalR (0.4.0 via nuget) and cannot find a way to force the server to force the client to disconnect. I guess I'm missing something obvious.
My test code is below, and I tried both Close () and Timeout () almost anywhere and combinations that I can think of without success. The client continues to receive impulse messages, although I always get 2 reconnections during the first 4-5 seconds that appear from return Connection.Close()inOnReceivedAsync()
Server:
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();
}
}
Customer:
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()
{
connection.Stop();
}
void OnReceive(string data)
{
Console.WriteLine(data);
}
}
Example client output:
d7615b15-f80c-4bc5-b37b-223ef96fe96c
d7615b15-f80c-4bc5-b37b-223ef96fe96c
d7615b15-f80c-4bc5-b37b-223ef96fe96c
...