Signalr 我可以设置2个信号机连接吗?
我有一个基本的聊天应用程序,设置如下:Signalr 我可以设置2个信号机连接吗?,signalr,Signalr,我有一个基本的聊天应用程序,设置如下: $(function () { // Set up references for other functions to call chatConnection = $.connection.chatHub; // Set up callbacks before starting server connection chatConnection.client.addNewMessageToPage = function (name, mes
$(function () {
// Set up references for other functions to call
chatConnection = $.connection.chatHub;
// Set up callbacks before starting server connection
chatConnection.client.addNewMessageToPage = function (name, message) {
var prettyMessage = name + ':' + message;
$('#chatHistory').append(prettyMessage);
$("#chatHistory").animate({ scrollTop: $('#chatHistory').prop("scrollHeight") }, 10);
};
// Start up connection to server, set up events
$.connection.hub.start().done(function () {
$('#sendChatButton').click(function () {
// Call the Send method on the hub.
chatConnection.server.sendMessage($('#displayName').val(), $('#chatBox').val());
// Clear text box and reset focus for next comment.
$('#chatBox').val('').focus();
});
});
});
这将调用我的ChatHub.cs服务器端,我将按照您的预期接收和传递消息
public void SendMessage(string name, string message)
{
Clients.All.addNewMessageToPage(name, message);
}
现在我想添加功能。我有一个与我的聊天中心几乎相同的新类,叫做“GameHub”,它的工作是处理移动,而不是处理聊天。到目前为止,我有这样的想法:
$(function () {
// Set up references for other functions to call
chatConnection = $.connection.chatHub;
gameConnection = $.connection.gameHub;
// Set up callbacks before starting server connection
chatConnection.client.addNewMessageToPage = function (name, message) {
var prettyMessage = name + ':' + message;
$('#chatHistory').append(prettyMessage);
$("#chatHistory").animate({ scrollTop: $('#chatHistory').prop("scrollHeight") }, 10);
};
gameConnection.client.receiveMove = function (name, move){
alert(name + ' played ' + move);
};
// Start up connection to server, set up events
$.connection.hub.start().done(function () {
$('#sendChatButton').click(function () {
// Call the Send method on the hub.
chatConnection.server.sendMessage($('#displayName').val(), $('#chatBox').val());
// Clear text box and reset focus for next comment.
$('#chatBox').val('').focus();
});
$('#sendMoveButton').click(function () {
gameConnection.server.sendMove(getMove());
});
});
});
但是没有任何东西能够进入服务器。这是因为我没有正确设置吗?signalR甚至可以支持2个集线器,还是应该是一个集线器,并从那里“辐射”到我的2个不同的功能区域?您可以,但如果没有性能差异,那么将这些功能放在一个集线器中就更简单了。所有数据都将命中所有集线器,因为它们共享相同的连接 如文件所述 由于没有提供,您可以检查您的游戏中心和方法,以确保在服务器端对它们进行了正确命名和装箱。可能的问题