SignalR没有从服务器调用客户端方法

时间:2013-11-14 17:38:41

标签: c# javascript jquery signalr

我在调用.start()之前添加了所有客户端方法。

我正在做的是加载所有客户端方法的连接,并且我有一个setTimeout 5秒实际调用.start(),所以我知道我的所有客户端脚本都已连线。

由于某种原因,脚本已连线,但不会被称为服务器。

现在,我能够毫无问题地调用服务器方法,我只需要服务器通过客户端脚本将该消息转发回客户端。

将客户端脚本放在命名函数对象中是否可以?

我删除了很多代码,因此它是可读的。

这样可行,因为我正在呼叫服务器。

$.connection.hub.proxies.collaboratorhub.server.sendMsg

-

这不起作用,因为我正等待服务器向客户端发送消息

 $.connection.hub.proxies.collaboratorhub.client.receiveMsg 

=

在我调试的服务器端,我正在从客户端向服务器接收sentMsg,并且正在从服务器调用receiveMsg,但它从未进入客户端。

    function ChatBox(collaboratorMenuItem) {

    var 
        sendMsg = function (ev) {
            // If the user has pressed enter

            if (ev.keyCode === 13) {

                if (this.value.trim() === '') { return false; };

                var avatar = attendees.querySelector('.' + meAttendee.profile.UserID);
                if (avatar) {

                    chats.appendChild(
                           docCreateAttrs('div', { 'class': 'collabChat', innerHTML: this.value }).addChild(avatar.cloneNode(true))
                           );
                } else {
                    chats.appendChild(
                          docCreateAttrs('div', { 'class': 'collabChat', innerHTML: this.value })
                          );
                };

                $.connection.hub.proxies.collaboratorhub.server.sendMsg({ msg: this.value });

                this.value = '';
                return false;
            } else { return true; };
        },



    debugger
    //Callbacks that are called from the server. Not intended for local function calls
    $.connection.hub.proxies.collaboratorhub.client.receiveMsg = function (cmsg) {

        var avatar = attendees.querySelector('.' + cmsg.uid);
        if (avatar) {

            chats.appendChild(
                   docCreateAttrs('div', { 'class': 'collabChat', innerHTML: cmsg.msg }).addChild(avatar.cloneNode(true))
                   );
        } else {
            chats.appendChild(
                  docCreateAttrs('div', { 'class': 'collabChat', innerHTML: cmsg.msg })
                  );
        };
    };
    $.connection.hub.proxies.collaboratorhub.client.addCurrAttendee = function (attendee) {

        debugger
        addAttendee(attendee);
    };
    $.connection.hub.proxies.collaboratorhub.client.joined = function (attendee) {
        debugger
        addAttendee(attendee);

        //let the new attendee know about you
        if (attendee.cnnid !== meAttendee.cnnid) {
            meAttendee.newbieid = attendee.cnnid;
            //
            this.server.addMeNewbie(meAttendee);
        };
    };
    $.connection.hub.proxies.collaboratorhub.client.rejoined = function (attendee) {
        debugger
        //console.log('Re joined : ' + attendee.cnnid);
        addAttendee(attendee);

        //let the new attendee know about you
        if (attendee.cnnid !== meAttendee.cnnid) {

            meAttendee.newbieid = attendee.cnnid;
            this.server.addMeNewbie(meAttendee);
        };
    };
    $.connection.hub.proxies.collaboratorhub.client.gone = function (attendee) {
        // console.log('gone : ' + attendee.cnnid);
        removeAttendee(attendee);
    };

    collaboratorMenuItem.onclick = function (e) {
        //debugger
        if (!chatBox.parentNode) {
            var xy = e.target.getBoundingClientRect();

            document.body.appendChild(chatBox.setGX(null, xy.left).setGY(null, xy.height));
            $(chatBox).draggable({ handle: chatHead, cursor: "move" }).css('position', 'absolute').resizable();
        } else {
            removeChat();
        };
    };

    return {
        chatBox: chatBox,
        attendLk: attendLk,
        addAttendee: addAttendee,
        removeAttendee: removeAttendee,
        removeChat: removeChat
    };
};

- 这就是我创建一个名为collaborateManager的名称函数的方法,当实例化时,它具有一个属性调用this.chatBox,这是所有客户端脚本加载的地方,在{{1 }}

collaboratorMenuItem只是ChatBox放在旁边的div。

ChatBox

在另一个全局JS文件中,我在五秒内开始连接。

  function collaborateManager(collaboratorMenuItem) {



                this.chatBox = new ChatBox(collaboratorMenuItem);


    }


    return {
        hideCollaborator: hideCollaborator,
        showCollaborator: showCollaborator,
        getCollaborator: getCollaborator,
        removeCollaboratorCollaborators: removeCollaborator
    };

});


每次连接都正常启动,这不是问题,我也可以发送服务器消息,我需要能够接收这些消息。

这是我的服务器端sendMsg被调用,但没有客户端收到receiveMsg()

document.bindReady(function () {

setTimeout(function () {

    //Global SignalR Connections State

    var cnnStateChanged = function (change) {

        if (change.newState === $.signalR.connectionState.reconnecting) {
            console.log('Re-connecting');
        }
        else if (change.newState === $.signalR.connectionState.connected) {

            console.log('The server is online');
        }
        else if (change.newState === $.signalR.connectionState.disconnected) {

            console.log('The server is offline');
        };
    },
     cnnReconnected = function (change) {

         if (change && change.newState === $.signalR.connectionState.reconnected) {

         };
         console.log('The server is re-connected');
     };
    //------------------------------------------------------------------------------------------------------------
    //Global start SignalR connectio

    $.connection.hub.stateChanged(cnnStateChanged);
    $.connection.hub.reconnected(cnnReconnected);

    if ($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected) {
        $.connection.hub.start().done(function () {

            //debugger
            // onConnected();
        });
    } else {
        //debugger
        // onConnected();
    };

},5000);

有什么建议吗?我做错了吗?

1 个答案:

答案 0 :(得分:0)

尝试将服务器端代码更改为:

public Task sendMsg(Cmsg cmsg)
{
   string pid = this.Context.QueryString["pid"],
   uid = this.Context.QueryString["uid"];
   //
   cmsg.cnnid = Context.ConnectionId;
   cmsg.uid = uid;
   //614
   return Clients.Caller.OthersInGroup(pid).receiveMsg(cmsg);
}

调用者中添加:return Clients.Caller.OthersInGroup(pid).receiveMsg(cmsg);