我在调用 .start() 之前添加了所有客户端方法。
我正在做的是加载所有客户端方法的连接,并且我有 5 秒的 setTimeout 来实际调用 .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
,这是加载所有客户端脚本的地方,在ChatBox
.
collaboratorMenuItem 只是将 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);
});
每次连接都正确启动,这不是问题,我也可以发送服务器消息,我需要能够接收这些消息。
这是我的服务器端 sendMsg 被调用,但没有客户端收到 receiveMsg()
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);
}
有什么建议么?我做错了吗?