【发布时间】:2013-11-14 17:38:40
【问题描述】:
我在调用 .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 文件中,我在 5 秒内开始连接。
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);
}
有什么建议吗?我做错了吗?
【问题讨论】:
-
我现在需要这方面的帮助,如果有人能提供帮助,那就太好了。扔给我一根骨头。
-
我能够让它工作,但我必须将所有启动的东西放在 .start() 中,直接放在正在连接的客户端事件下,这并不理想.我希望在一页上进行两种不同类型的聊天,我的想法是连接所有客户端的内容,然后当我知道所有内容都已加载时调用 .start(),我应该很高兴。除非有人能告诉我我真的没有正确地做到这一点,否则我会闻到一些 iFrame 的气味。而且,哦,我多么喜欢那些讨厌的小 iFrame。
标签: c# javascript jquery signalr