简体   繁体   English

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

[英]SignalR OnConnected - Sending Connected Client a Message

Very simple question. 很简单的问题。 Why does the client that has just connected not receive any messages, but all other clients get the broadcast? 为什么刚刚连接的客户端没有收到任何消息,但所有其他客户端都接收到广播? What's the correct way of sending the client that connected a message? 发送连接消息的客户端的正确方法是什么?

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));      
}

I think you should put a call back function in a connection.start() function in your javascript and resend your command. 我想你应该在你的javascript中的connection.start()函数中添加一个回调函数并重新发送你的命令。 Separate type of broadcast message and echo message in JSON may help you detect status of connection after you join in... The bellows work for me.. JSON中单独类型的广播消息和回显消息可以帮助您在加入后检测连接状态...波纹管对我有用..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM