Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SignalR动态创建多个集线器_C#_Jquery_Asp.net_Asp.net Mvc 5_Signalr - Fatal编程技术网

C# 使用SignalR动态创建多个集线器

C# 使用SignalR动态创建多个集线器,c#,jquery,asp.net,asp.net-mvc-5,signalr,C#,Jquery,Asp.net,Asp.net Mvc 5,Signalr,我目前正在使用signal聊天。我有一个表单,用户会将特定的订单加载到表单中。这种情况下,其他员工可能会搜索该订单号,因此我只希望这些人参与聊天。当前,如果加载站点,每个人都在一个名为ChatHub的hub中 ChatHub.cs: public class ChatHub: Hub { public void Send(string name, string message) { // Call the addNewMessageToPage method to update clien

我目前正在使用
signal
聊天。我有一个表单,用户会将特定的订单加载到表单中。这种情况下,其他
员工可能会搜索该订单号,因此我只希望这些人参与聊天。当前,如果加载站点,每个人都在一个名为
ChatHub
hub中

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);
 }
}
}
@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>
Chat.cshtml:

public class ChatHub: Hub {
 public void Send(string name, string message) {
  // Call the addNewMessageToPage method to update clients.
  Clients.All.addNewMessageToPage(name, message);
 }
}
}
@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>
@节脚本{
$(函数(){
//为中心引用自动生成的代理。
var chat=$.connection.chatHub;
//创建一个中心可以回调以显示消息的函数。
chat.client.addNewMessageToPage=函数(名称、消息){
//将消息添加到页面。
$(“#讨论”).append(htmlEncode(名称)
+“:”+htmlEncode(消息)+'\n');
};
//获取用户名并将其存储到消息前。
//$('#displayname').val(提示('输入您的姓名:','');
//将初始焦点设置为消息输入框。
$(“#消息”).focus();
//启动连接。
$.connection.hub.start().done(函数(){
$('#sendmessage')。单击(函数(){
//在集线器上调用Send方法。
chat.server.send($('#displayname').val(),$('#message').val());
//清除文本框并重置下一条注释的焦点。
$('#message').val('.focus();
});
});
});
//此可选函数用于对页面中显示的消息进行html编码。
函数htmlEncode(值){
var encodedValue=$('').text(value.html();
返回编码值;
}
}
我尝试添加以下内容:

public class ChatHub: Hub {
 public void Send(string name, string message) {
  // Call the addNewMessageToPage method to update clients.
  Clients.All.addNewMessageToPage(name, message);
 }
}
}
@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>

$(函数(){
var chat=jQuery.connection.chat;
chat.addMessage=功能(消息、房间){
if($('#currentRoom').val()==房间){
$('#messagesList')。追加(message);
}
};
发送($('textboxMessage').val(),$('currentRoom').val());
$('#textboxMessage').val(“”);
$.connection.hub.start();
});

我正试图找到一种方法,根据用户加载订单来获取多个集线器或聊天室。

您必须使用组:

您必须与团队合作:

您必须根据打开的订单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>
@节脚本{
$(函数(){
//为中心引用自动生成的代理。
var orderId='@Model.Id'//您可以更改此行以从正确的位置获取orderId
var chat=$.connection.chatHub;
//创建一个中心可以回调以显示消息的函数。
chat.client.addNewMessageToPage=函数(名称、消息){
//将消息添加到页面。
$(“#讨论”).append(htmlEncode(名称)
+“:”+htmlEncode(消息)+'\n');
};
//获取用户名并将其存储到消息前。
//$('#displayname').val(提示('输入您的姓名:','');
//将初始焦点设置为消息输入框。
$(“#消息”).focus();
//启动连接。
$.connection.hub.start().done(函数(){
$('#sendmessage')。单击(函数(){
//在集线器上调用Send方法。
chat.server.send($('#displayname').val(),$('#message').val());
//清除文本框并重置下一条注释的焦点。
$('#message').val('.focus();
chat.server.joinOrderGroup($('#displayname').val(),orderId);
});
});
});
//此可选函数用于对页面中显示的消息进行html编码。
函数htmlEncode(值){
var encodedValue=$('').text(value.html();
返回编码值;
}
}

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

您必须根据打开的订单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>
@节脚本{
$(函数(){
//为中心引用自动生成的代理。
var orderId='@Model.Id'//您可以更改此行以从正确的位置获取orderId
var chat=$.connection.chatHub;
//创建一个中心可以回调以显示消息的函数。
chat.client.addNewMessageToPage=函数(名称、消息){
//将消息添加到页面。
$(“#讨论”).append(htmlEncode(名称)
+“:”+htmlEncode(消息)+'\n');
};
//获取用户名并将其存储到消息前。
//$('#displayname').val(提示('输入您的姓名:','');
//将初始焦点设置为消息输入框。
$(“#消息”).focus();
//启动连接。
$.connection.hub.start().done(函数(){
$('#sendmessage')。单击(函数(){
//在集线器上调用Send方法。
chat.server.send(