javascript - SignalR 一个连接多个集线器,多个处理程序
问题描述
理想情况下,我希望与多个集线器建立一个连接,但在连接开始后注册客户端处理程序。
什么有效:
- 每个集线器有多个连接并在连接之前注册处理程序 - 我想减少尽可能多的连接,也可能不知道/想要事先注册所有处理程序
- 每次注册新消息时都必须停止和开始 - 这是有问题的,因为您可能会在其他地方中断另一条消息
//multiple connections method = works
var _page_connection = $.hubConnection();
var _page_connection2 = $.hubConnection();
function connectionOne() {
var proxy1 = _page_connection.createHubProxy('hub_Echo');
if (_page_connection && _page_connection.state === $.signalR.connectionState.disconnected) {
proxy1.on('echo', function (msg) { console.log('Echo1: ' + msg); });
_page_connection.start()
.done(function () { console.log('Now connected, connection ID=' + _page_connection.id); })
.fail(function () { console.log('Could not connect'); });
} else {
console.log('Already connected, connection ID=' + _page_connection.id);
proxy1.on('echo', function (msg) { console.log('Echo1: ' + msg); });
_page_connection.start()
.done(function () { console.log('Connected again, connection ID=' + _page_connection.id); })
.fail(function () { console.log('Could not connect'); });
}
$('#btn_echosend').on('click', function () {
proxy1.invoke('echo', 'marco');
});
}
function connectionTwo() {
var proxy2 = _page_connection2.createHubProxy('hub_Echo2');
if (_page_connection2 && _page_connection2.state === $.signalR.connectionState.disconnected) {
proxy2.on('echo2', function (msg) { console.log('Echo2: ' + msg); });
_page_connection2.start()
.done(function () { console.log('Now connected, connection ID=' + _page_connection2.id); })
.fail(function () { console.log('Could not connect'); });
} else {
console.log('Already connected, connection ID=' + _page_connection.id);
proxy2.on('echo2', function (msg) { console.log('Echo2: ' + msg); });
_page_connection2.start()
.done(function () { console.log('Connected again, connection ID=' + _page_connection2.id); })
.fail(function () { console.log('Could not connect'); });
}
$('#btn_echosend2').on('click', function () {
proxy2.invoke('echo2', 'polo');
});
}
什么不起作用:
- 使用“在客户端定义方法(没有生成的代理,或者在调用启动方法后添加)”的官方文档 https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to- the-api/hubs-api-guide-javascript-client#how-to-get-a-proxy-for-a-hub-class - 仅当处理程序添加到之前已经存在的同一个集线器连接时才有效开始了。如果您开始连接到新的集线器,新的处理程序将不会注册,因为该集线器之前没有附加到 OnConnect 的处理程序
- 偶然发现了@Steve Greatrex网站关于同样的问题 ,但他的实施也没有奏效。我可能做错了什么
function SignalRClient(methods) {
this._handlers = {};
methods.forEach(this.registerHandler.bind(this));
}
SignalRClient.prototype.invokeHandler = function (name) {
var handler = this._handlers[name];
if (handler) {
var handlerArgs = Array.prototype.slice.call(arguments, 1);
handler.apply(this, handlerArgs);
}
};
SignalRClient.prototype.registerHandler = function (name) {
var getter = this.invokeHandler.bind(this, name);
Object.defineProperty(this, name, {
enumerable: true,
get: function () {
return getter;
},
set: function (value) {
this._handlers[name] = value;
}.bind(this)
});
};
function connectionTwo() {
$.connection.hub_Echo2.client = new SignalRClient(['echo', 'echo2']);
$.connection.hub_Echo2.connection.start().done(function () { console.log('Now connected, connection ID=' + $.connection.hub.id); })
$.connection.hub_Echo2.client.echo = function (data) { console.log('Echo2: ' + data); };
$('#btn_echosend2').on('click', function () {
$.connection.hub_Echo2.server.echo('Polo');
});
}
function connectionOne() {
$.connection.hub_Echo.client = new SignalRClient(['echo', 'echo2']);
$.connection.hub_Echo.connection.start().done(function () { console.log('Now connected, connection ID=' + $.connection.hub.id); })
$.connection.hub_Echo.client.echo = function (data) { console.log('Echo: ' + data); };
$('#btn_echosend').on('click', function () {
$.connection.hub_Echo.server.echo('Marco');
});
}
解决方案
推荐阅读
- django - Django为什么只有基于字符串的字段不能有null = true?
- python - 如何在 selenium python 中更改集成测试的优先级
- c++ - OpenCv 预装在 google Colab 中,但我想从 opencv_contrib 添加一个额外的模块。如何做到这一点?
- docker - Docker driver_opts.type=none?
- javascript - 无法将多个图像传递给服务器 Nodejs
- flutter - 防止颤动文本表单字段验证消息省略号
- r - R Shiny `options(shiny.autoreload = TRUE)` 不适用于模块脚本中的更改
- ios - 从函数外部更改 UILabel 的文本
- linux - postgres用户的很多tracepath命令
- javascript - 如何合并异步函数的回调结果