SignalR OnConnected - 向连接的客户端发送消息

Ser*_*rge 10 c# asp.net signalr

很简单的问题.为什么刚刚连接的客户端没有收到任何消息,但所有其他客户端都接收到广播?发送连接消息的客户端的正确方法是什么?

protected override void OnConnected(HttpContextBase context, string clientId)
{      
    GameAction message = new GameAction();
    message.text = "Player connected";
    Connection.Broadcast(serializer.Serialize(message));

    GameAction gamestate = new GameAction(); 
    gamestate.text = "Some client specific info";     
    Send(clientId, serializer.Serialize(gamestate));      
}
Run Code Online (Sandbox Code Playgroud)

hoa*_*17b 3

我认为你应该在 javascript 的 connection.start() 函数中放置一个回调函数并重新发送你的命令。JSON 中不同类型的广播消息和回显消息可以帮助您在加入后检测连接状态...波纹管对我有用..

.....
connection.start({ callback: function () {
var username = getCookie("username");
connection.send(window.JSON.stringify({ type: 0, value: username }));
}
});
......
Run Code Online (Sandbox Code Playgroud)