我正在用 Java 编写自己的 SignalR 客户端,但遇到了一些麻烦。
首先我想实现 PersistentConnection 逻辑。我的服务器代码取自示例:
public class Battle : PersistentConnection
{
protected override Task OnConnectedAsync(IRequest request, string connectionId)
{
return Connection.Broadcast("Connection " + connectionId + " connected");
}
protected override Task OnReconnectedAsync(IRequest request, IEnumerable<string> groups, string clientId)
{
return Connection.Broadcast("Client " + clientId + " re-connected");
}
protected override Task OnReceivedAsync(IRequest request, string connectionId, string data)
{
// return Connection.Broadcast("Connection " + connectionId + " sent ");
return Connection.Send(connectionId, "Connection " + connectionId + " sent ");
}
protected override Task OnDisconnectAsync(string connectionId)
{
return Connection.Broadcast("Connection " + connectionId + " disconncted");
}
protected override Task OnErrorAsync(Exception error)
{
return Connection.Broadcast("Error occured " + error);
}
}
从.NET客户端代码来看,我理解为了连接到服务器客户端应该:
1) 向响应发送请求http://myserver/battle/negotiate
并ConnectionId
从响应中获取
2) 发送请求到http://myserver/battle/connect?transport=longPolling&connectionId=<received_connection_id>
我的问题是客户应该做什么来保持连接?它应该如何收听服务器广播消息?
另一个问题是,在建立连接后尝试将消息从客户端发送到服务器时,我没有收到任何响应。我发送请求到http://myserver/battle/send?transport=longPolling&connectionId=<received_connection_id>
。OnReceivedAsync
总是调用方法,但我没有得到响应(与发送的数据无关)。
对于我的问题和 SignalR 工作的内部原则的任何解释,我将不胜感激。提前致谢。