.start() を呼び出す前に、すべてのクライアント メソッドを追加しています。
私がやっていることは、すべてのクライアント メソッドの接続をロードすることであり、実際に .start() を呼び出すために 5 秒間 setTimeout を取得しているので、すべてのクライアント スクリプトが接続されていることがわかります。
何らかの理由で、スクリプトが接続されますが、サーバーとして呼び出されません。
これで、問題なくサーバー メソッドを呼び出すことができます。必要なのは、サーバーがクライアント スクリプトを介してそのメッセージをクライアントに中継することだけです。
名前付き関数オブジェクト内にクライアント スクリプトを配置しても問題ありませんか?
読みやすいように、多くのコードを削除しました。
サーバーを呼び出しているため、これは機能します。
$.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);
}
助言がありますか?私はこれを間違っていますか?