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

SignalR not calling client methods from the server

本文关键字:客户端 方法 调用 服务器 SignalR      更新时间:2023-09-26

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

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

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

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

是否可以

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

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

这有效,因为我正在调用服务器。

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

-

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

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

=

在服务器端,当我调试时,我正在从客户端接收到服务器的sentMsg,并且接收Msg是从服务器调用的,但它永远不会到达客户端。

    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,这是加载所有客户端脚本的地方,在ChatBox中。

协作者菜单项只是聊天框旁边的一个div。

  function collaborateManager(collaboratorMenuItem) {

                this.chatBox = new ChatBox(collaboratorMenuItem);

    }

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

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

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);

});


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

这是我的服务器端发送Msg被调用,但没有客户端收到接收Msg()

  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.OthersInGroup(pid).receiveMsg(cmsg);
    }

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

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

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);
}

调用方添加到:返回 Clients.Caller.OthersInGroup(pid).receiveMsg(cmsg);