2012-07-09 158 views
0

有人能告訴我我的代碼有什麼問題嗎?我試圖從一個用戶發送一個私人消息到另一個使用socket.io。我認爲這個問題是與線socket.io發送消息給特定用戶

io.sockets.socket(targetUser).emit('private message', tstamp(), socket.nickname, linkify(msg)); 

的問題是,該消息沒有得到通過向客戶端,或者至少沒有得到追加到div地方。我不知道如何解決問題的確切位置。

相關的代碼如下 - 我可以添加整個代碼,如果這還不夠..:

感謝您的幫助。我正在學習node.js和socket.io - 雖然速度很慢!在客戶端上

(發送):

function startPrivateChat(targetUser, sendingUser){ 
     if(targetUser !=sendingUser){ 
     var privateMessage1="starting private chat with " + targetUser + ". My name is " + sendingUser; 
     var chatBoxId= targetUser + 'ChatBox'; 
     $("#content").append('<div class="chatBox" id="' + chatBoxId + '">' + sendingUser + ', ' + targetUser + '</div>'); 
     $('#' + chatBoxId).append('new private chat'); 
     $('#' + chatBoxId).dialog(); 
     privateMessage(tstamp(), myNick, privateMessage1); 
     //socket.emit('private message', targetUser, privateMessage1); 
     clear();  
     } 
    } 

服務器:

socket.on('private message', privateMessage); 
function privateMessage (msg_time, from, msg) { 
     $('#chatLogDiv').append($('<p>').append($('<small>').text(msg_time)).append($('<b>').text(from), linkify(msg))); 
    } 
+0

你能添加的完整代碼,最好是在引擎收錄 – almypal 2012-07-09 04:23:58

+0

chat_server.js [鏈接](HTTP: //pastebin.com/nxHdzHDg) example_chat.tpl.php [鏈接](http://pastebin.com/t0h7rSFv) – 2012-07-09 04:31:53

+0

代碼運行良好。所做的修改是包含jquery庫並排除調用drupal_add_library和drupal_add_js方法的php代碼。 – almypal 2012-07-09 05:44:36

回答

0

我得到這個工作:

客戶端(收到)上
socket.on('private message', function(targetUser,msg) {   
    io.sockets.socket(targetUser).emit('private message', tstamp(), socket.nickname, linkify(msg)); 
    updateLog('private message', socket.nickname, msg); 
    }); 

在的node.js服務器:

socket.on("SendMessage", function (value) { 
     var userIdToRecieve = theOnlyUserToRecieveString; 
     io.sockets.emit("SendAMessage", socket.userId, value, userIdToRecieve); 
    }); 

於JavaScript側的客戶端:

socket.on("SendAMessage", function (userId, value, userIdToRecieve) { 
    // Write to the conversation window 

    if (userIdToRecieve == myLocalUserID) { 
     console.log("I shalleth recieve"); 
    } 

}); 
相關問題