【问题标题】:Creating multiple hubs dynamically using SignalR使用 SignalR 动态创建多个集线器
【发布时间】:2016-10-09 07:07:53
【问题描述】:

我目前正在使用SignalR 进行聊天。我有一个表格,用户会将特定订单加载到表格中。场景是其他staff members 可能会搜索该订单号,因此我只希望那些人在聊天中。目前,如果您加载网站,每个人都在一个名为 ChatHubhub 中。

ChatHub.cs:

public class ChatHub: Hub {
 public void Send(string name, string message) {
  // Call the addNewMessageToPage method to update clients.
  Clients.All.addNewMessageToPage(name, message);
 }
}
}

Chat.cshtml:

@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-2.0.3.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.-->
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page.
                $('#discussion').append(htmlEncode(name)
                    + ':' + htmlEncode(message) + '\n');
            };
            // Get the user name and store it to prepend to messages.

            //$('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}
<script>

我已尝试添加以下内容:

<script>
$(function () {
    var chat = jQuery.connection.chat;

    chat.addMessage = function (message, room) {

        if ($('#currentRoom').val() == room) {
            $('#messagesList').append(message);
        }
    };


        chat.send($('#textboxMessage').val(), $('#currentRoom').val());
        $('#textboxMessage').val("");


    $.connection.hub.start();
});
</script>

我正在尝试根据用户加载订单来寻找获得多个中心或聊天室的方法。

【问题讨论】:

  • 您有问题吗?目前你只是在说你做了什么,但你想要实现什么或者问题是什么。
  • 是的,我正在尝试找到一种方法来创建特定于订单的聊天。所以 2 人加载订单,只有他们才能成为聊天的一部分。但如果第三个人加载它,他们也是其中的一部分。

标签: c# jquery asp.net asp.net-mvc-5 signalr


【解决方案1】:

你必须根据打开的订单id将用户分组,更改中心并添加一个方法如下:

public class ChatHub: Hub {
 public void Send(string name, string message,string orderId) {
  // Call the addNewMessageToPage method to update clients.
  Clients.Group(orderId).addNewMessageToPage(name, message);
 }

public void JoinOrderGroup(string name,string orderId)
 {
     Groups.Add(Context.ConnectionId, orderId);
 }
}

然后修改您的 JavaScript 以在用户打开页面时调用“JoinOrderGroup”。

@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-2.0.3.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.-->
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.
             var orderId = '@Model.Id' //You can change this line to get the orderId from the correct place
             var chat = $.connection.chatHub;
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page.
                $('#discussion').append(htmlEncode(name)
                    + ':' + htmlEncode(message) + '\n');
            };
            // Get the user name and store it to prepend to messages.

            //$('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                  chat.server.joinOrderGroup($('#displayname').val(),orderId);
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}
<script>

这样,当页面启动和hub连接时,它会发送一条消息加入表单中与订单相关的组,并且所有对发送消息方法的后续调用都将包含订单id,它将是仅按此顺序传播给用户。

【讨论】:

  • 您如何确定集团中的成员?
  • 群是你问题中的订单号,每个打开订单界面的用户应该可以和看到相同订单的其他用户聊天吧?
  • 是的,有不同订单的人将无法成为同一个聊天的一部分。您可以更新退出时删除此人的代码吗?
  • @PriceCheapterton 在 Hub 端,当客户端连接或断开连接时,您会收到通知。它们提供了一个 ConnectionId,您可以使用它来从组中删除断开连接的用户。
  • 而且我相信每次用户连接都会有一个单独的Id
【解决方案2】:
猜你喜欢
  • 2015-05-17
  • 2019-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
相关资源
最近更新 更多