17

asp.net クラシック Web サイトがあります。SignalR の基本機能が動作するようになりました (1 つのクライアントが残りのクライアントにメッセージを送信する場合)。しかし、今は特定の接続 ID にのみメッセージを送信したいと考えています。

私のハブ:

**    [HubName("chatHub")]
    public class ChatHub : Hub 
    {
        public static List<string> messages = new List<string>();

        public void GetServiceState()
        {
            Clients.updateMessages(messages);
        }

        public void UpdateServiceState()
        {
            messages.Add(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

            Clients.updateMessages(messages);
        }

    }**

ASP コード:

        <script type="text/javascript">
            $(function () {
                // creates a proxy to the health check hub

                var healthCheckHub = $.connection.chatHub;
                console.log($.connection.hub)
                // handles the callback sent from the server
                healthCheckHub.updateMessages = function (data) {
                    $("li").remove();

                    $.each(data, function () {
                        $('#messages').append('<li>' + this + '</li>');
                        console.log($.connection);
                    });
                };

                $("#trigger").click(function () {
                    healthCheckHub.updateServiceState();
                });

                // Start the connection and request current state
                $.connection.hub.start(function () {
                    healthCheckHub.getServiceState();
                });


            });

Clients.updateMessages(メッセージ); それらすべてにメッセージを送信します。どうすればこれを解決できますか?

PS: すでに見ました: Signalr/PersistentConnection を使用して、接続されたクライアントにサーバー メッセージを送信します。

およびhttp://riba-escapades.blogspot.dk/2012/05/signalr-send-messages-to-single-client.html

それはうまくいきませんでした。

4

1 に答える 1

33

さて、あなたは次のようにハブから単一のクライアントにメッセージを送ることができます:

Clients.Client(someConnectionIdIWantToSendToSpecifically).doSomething();

秘訣は、メッセージの送信先となる接続IDを知っている必要があることです。さらに具体的には、メッセージを送信したいものの論理IDも知りたいと思うでしょう。なぜなら、その論理IDは複数の接続を持っているか、完全に異なる接続IDでドロップして再接続した可能性があるからです。論理IDへの接続のマッピングは、SignalRがアプリケーション自体に任せるものです。

于 2012-11-12T18:48:16.423 に答える