我正在Java上編寫自己的SignalR客戶端,並且遇到一些麻煩。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工作的內部原理有任何解釋,我將不勝感激。 在此先感謝。
你看看.NET客戶端實現嗎? – davidfowl
@dfowler是的,但我是.NET新手,很難理解C#代碼 –
Java和.NET非常接近 – davidfowl