如何连接到signalR服务器并在AJax调用的回调中将我的客户端方法注册到我的signalR server



这是我的工作代码:

$(function() {
$.connection.hub.url = 'sampleUrl';
$.connection.hub.start().done(function() {
var uid = settingInvoiceUID;
if (uid !== '')
$.connection.notifyHub.server.registerInHub(uid);
});
$.connection.notifyHub;
$.connection.notifyHub.client.updateClient = function(gInfo) {
alret('blah blah blah');
}
});

我想这样使用它,一切都会正常工作,只是服务器无法在以下代码中调用此方法updateClient

$("[id*='qrCodeImage']").click(function() {
var postUrl = '/sample.aspx/SetAsScanned';
var postedData = "{data: '" + settingInvoiceUID + "'}";
$.ajax({
type: 'POST',
url: postUrl,
dataType: 'json',
data: postedData,
contentType: "application/json; charset=utf-8",
success: function(data) {
var result = JSON.parse(data.d);
if (result.IsSuccess) {
$.connection.hub.url = 'sampleUrl';
$.connection.hub.start().done(function() {
var uid = settingInvoiceUID;
if (uid !== '')
$.connection.notifyHub.server.registerInHub(uid);
});
$.connection.notifyHub;
$.connection.notifyHub.client.updateClient = function(gInfo) {
alret(gInfo);
}
});
});

是否可以在回调中设置所有SignalR代码,包括连接和集线器注册以及clientMethod?

$.getScript(settingSignalRServerPath + "/hubs", function () {
$.connection.hub.url = settingSignalRServerPath;
// Declare a proxy to reference the hub.
simpleHubProxy = $.connection.notifyHub;
//Register to the "updateClient" callback method of the hub
//This method is invoked by the hub
simpleHubProxy.client.updateClient = function (gInfo) {
checkTransactionAndRedirectToMerchant(gInfo);
}
$.connection.hub.start()
.done(function () {
var uid = '12345';
$.connection.notifyHub.server.registerInHub(uid);
})
.fail(function () {
console.log('creating conn failed')
});
});

最后我做到了。u可以将整个代码复制到回调中调用的方法:D

最新更新