0

我已经更新了我的服务器解决方案以使用 MVC 5 和最新版本的信号器,并且有两个主要问题:

  1. 我已经覆盖了该OnConnected函数并且它没有被调用。
  2. 没有收到发给客户端的消息,正在接收来自客户端的呼叫

这是客户端代码

define(['jquery', 'toastr', 'Q'], function($, toastr, Q) {

var incidentHubProxy;
var deferred = Q.defer();

var connect = function() {
    var connection = $.hubConnection(localStorage.url);
    $.connection.hub.logging = true;
    incidentHubProxy = connection.createHubProxy('notification');

    connection.start()
            .done(function () {
                toastr.success('Now connected, connection ID=' + connection.id);
                setInterval(function () {
                    incidentHubProxy.invoke('ping');
                }, 3000);
                deferred.resolve();
            })
            .fail(function () { toastr.error('Could not connect'); });
    incidentHubProxy.on('notify', function (data) {
        toastr.info(data.topic);
        toastr.info(data.data);
    });
    incidentHubProxy.on('pong', function (data) {
        toastr.info('got pong');
    });

    return deferred.promise;
};

var joinGroup = function (groupName) {
    incidentHubProxy.invoke('joinGroup', groupName);
};



return {
    connect: connect,
    joinGroup: joinGroup
};
});

我已将代码更新为该代码,但仍然无法正常工作

定义(['jquery','toastr','Q'],函数($,toastr,Q){

var incidentHubProxy;
var deferred = Q.defer();

var connect = function() {
    var connection = $.hubConnection(localStorage.url);
    $.connection.hub.logging = true;
    incidentHubProxy = connection.createHubProxy('notification');

    incidentHubProxy.notify = function(data) {
        toastr.info(data.topic);
        toastr.info(data.data);
    };

    incidentHubProxy.pong = function(data) {
        toastr.info('got pong');
    };

    connection.start()
            .done(function () {
                toastr.success('Now connected, connection ID=' + connection.id);
                setInterval(function () {
                    incidentHubProxy.invoke('ping');
                }, 3000);
                deferred.resolve();
            })
            .fail(function () { toastr.error('Could not connect'); });


    return deferred.promise;
};

var joinGroup = function (groupName) {
    incidentHubProxy.invoke('joinGroup', groupName);
};



return {
    connect: connect,
    joinGroup: joinGroup
};

});

4

0 回答 0