如何向现有连接添加额外的处理程序?
How to add additional handlers to an existing connection?
在我的 MVC 5 应用程序中,我在页面加载时在客户端设置 Signal R 连接,这按预期工作。
稍后我想添加一个额外的处理程序并进行服务器端调用,我可以看到服务器收到此调用然后启动一些客户端调用,客户端的处理程序不会被调用.
页面加载时的连接设置
function initialiseRealTimeDataRetrieval() {
var hub = $.connection.autoGeneratedProxyForHub;
hub.client.recieveRealTimeData = function (data) {
//Do Stuff
};
$.connection.hub.start().done(function () {
hub.server.getRealTimeData();
});
}
稍后进行的其他调用
function initialiseFeed () {
var hub = $.connection.autoGeneratedProxyForHub;
hub.client.recieveRealTimeDataFeed = function (data) {
//Do stuff
};
if ($.connection.hub.state == $.connection.connectionState.connected) {
hub.server.getRealTimeDataFeed();
}
else {
$.connection.hub.start().done(function () {
hub.server.getRealTimeDataFeed();
});
}
}
到目前为止,我已经尝试了以下方法:
- 确保在服务器上调用从客户端到服务器的调用。
- 如果附加调用与页面加载时执行的调用和处理程序一起进行,请确保它们按预期工作。
- 查看文档以了解是否必须重新启动连接才能注册新的处理程序。
- 尝试了在添加新处理程序后重新启动连接的各种方法
对于额外的调用,下面的方法按预期工作,但是在页面加载时为连接所做的一切都是多余的:
function initialiseFeed () {
var hub = $.connection.autoGeneratedProxyForHub;
hub.client.recieveRealTimeDataFeed = function (data) {
//Do stuff
};
$.connection.hub.stop();
$.connection.hub.start().done(function () {
hub.server.getRealTimeDataFeed();
});
}
通过调试器检查集线器对象确实显示所有客户端都已连接,包括其他客户端。
根据 Signal R JS API Docs,自动生成的集线器代理不能用于注册多个事件处理程序:
When to use the generated proxy
If you want to register multiple event handlers for a client method
that the server calls, you can't use the generated proxy. Otherwise,
you can choose to use the generated proxy or not based on your coding
preference. If you choose not to use it, you don't have to reference
the "signalr/hubs" URL in a script element in your client code.
还要为现有连接注册新的处理程序,在建立连接之前,该连接必须至少有一个与之关联的处理程序,在注册新的处理程序时,您必须调用 start()
:
Note
Normally you register event handlers before calling the start method
to establish the connection. If you want to register some event
handlers after establishing the connection, you can do that, but you
must register at least one of your event handler(s) before calling the
start method. One reason for this is that there can be many Hubs in an
application, but you wouldn't want to trigger the OnConnected event on
every Hub if you are only going to use to one of them. When the
connection is established, the presence of a client method on a Hub's
proxy is what tells SignalR to trigger the OnConnected event. If you
don't register any event handlers before calling the start method, you
will be able to invoke methods on the Hub, but the Hub's OnConnected
method won't be called and no client methods will be invoked from the
server.
在我的 MVC 5 应用程序中,我在页面加载时在客户端设置 Signal R 连接,这按预期工作。
稍后我想添加一个额外的处理程序并进行服务器端调用,我可以看到服务器收到此调用然后启动一些客户端调用,客户端的处理程序不会被调用.
页面加载时的连接设置
function initialiseRealTimeDataRetrieval() {
var hub = $.connection.autoGeneratedProxyForHub;
hub.client.recieveRealTimeData = function (data) {
//Do Stuff
};
$.connection.hub.start().done(function () {
hub.server.getRealTimeData();
});
}
稍后进行的其他调用
function initialiseFeed () {
var hub = $.connection.autoGeneratedProxyForHub;
hub.client.recieveRealTimeDataFeed = function (data) {
//Do stuff
};
if ($.connection.hub.state == $.connection.connectionState.connected) {
hub.server.getRealTimeDataFeed();
}
else {
$.connection.hub.start().done(function () {
hub.server.getRealTimeDataFeed();
});
}
}
到目前为止,我已经尝试了以下方法:
- 确保在服务器上调用从客户端到服务器的调用。
- 如果附加调用与页面加载时执行的调用和处理程序一起进行,请确保它们按预期工作。
- 查看文档以了解是否必须重新启动连接才能注册新的处理程序。
- 尝试了在添加新处理程序后重新启动连接的各种方法
对于额外的调用,下面的方法按预期工作,但是在页面加载时为连接所做的一切都是多余的:
function initialiseFeed () {
var hub = $.connection.autoGeneratedProxyForHub;
hub.client.recieveRealTimeDataFeed = function (data) {
//Do stuff
};
$.connection.hub.stop();
$.connection.hub.start().done(function () {
hub.server.getRealTimeDataFeed();
});
}
通过调试器检查集线器对象确实显示所有客户端都已连接,包括其他客户端。
根据 Signal R JS API Docs,自动生成的集线器代理不能用于注册多个事件处理程序:
When to use the generated proxy
If you want to register multiple event handlers for a client method that the server calls, you can't use the generated proxy. Otherwise, you can choose to use the generated proxy or not based on your coding preference. If you choose not to use it, you don't have to reference the "signalr/hubs" URL in a script element in your client code.
还要为现有连接注册新的处理程序,在建立连接之前,该连接必须至少有一个与之关联的处理程序,在注册新的处理程序时,您必须调用 start()
:
Note
Normally you register event handlers before calling the start method to establish the connection. If you want to register some event handlers after establishing the connection, you can do that, but you must register at least one of your event handler(s) before calling the start method. One reason for this is that there can be many Hubs in an application, but you wouldn't want to trigger the OnConnected event on every Hub if you are only going to use to one of them. When the connection is established, the presence of a client method on a Hub's proxy is what tells SignalR to trigger the OnConnected event. If you don't register any event handlers before calling the start method, you will be able to invoke methods on the Hub, but the Hub's OnConnected method won't be called and no client methods will be invoked from the server.