2014-10-06 198 views
0

我正在使用socket.io和node.js。我能夠從服務器向所有客戶端廣播消息,但是我在從服務器向特定客戶端發送消息時遇到問題。 我是新來這個socket.io和Node.js的使用socket.io和node.js將消息從服​​務器發送到客戶端

下面

被剪斷的代碼,爲客戶和服務

服務器代碼:

var http = require('http'), 
fs = require('fs'); 

var express = require('express'); 

var app = http.createServer(function (request, response) { 
fs.readFile("client.html", 'utf-8', function (error, data) { 

if(error) 
    { 
      responce.writeHead(404); 
      responce.write("File does not exist"); 
      responce.end(); 
    } 
    else 
    { 
      response.writeHead(200, {'Content-Type': 'text/html'}); 
      response.write(data); 
      response.end(); 
    } 
}); 
}).listen(1337); 


var io = require('socket.io').listen(app); 


var clients = [ ] ; 
var socketsOfClients = {}; 

io.sockets.on('connection', function(socket) 
{ 


    socket.on('message_to_server', function(data) 
    { 


      clients.push(socket.id); 

      socket(clients[0]).emit("message_to_client" , { message: data["message"] }); 

    }); 
}); 

客戶代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <script src="/socket.io/socket.io.js"></script> 
    <script type="text/javascript"> 
     // our socket.io code goes here 

     var socketio = io.connect("127.0.0.1:1337"); 

     socketio.on("message_to_client", function(data) { 
     document.getElementById("chatlog").innerHTML = ("<hr/>" + 
     data['message'] + document.getElementById("chatlog").innerHTML); 
     }); 

     function sendMessage() { 
     var msg = document.getElementById("message_input").value; 
     socketio.emit("message_to_server", { message : msg}); 
     } 
    </script> 
</head> 
<body> 
    <input type="text" id="message_input"/> 
    <button onclick="sendMessage()">send</button> 
    <div id="chatlog"></div> 
</body> 
</html> 

當我執行,它給錯誤,如:

socket(clients[0]).emit("message_to_client" , { message: data["message"] }); 
    ^

類型錯誤:對象不是在插座的功能 。

回答

0

我想我明白你想要做什麼。您想通過使用其ID來將消息發送到特定的套接字。

根據文檔socket功能(http://socket.io/docs/server-api/#socket),所以調用它像socket()導致您的代碼失敗。

而不是存儲在陣列中的插座,讓我們試着將它們存儲在一個哈希來代替:

var clients = {}; 

io.sockets.on('connection', function (socket) { 

    // store a reference to this socket in the hash, using 
    // its id as the hash key 
    clients[socket.id] = socket; 

    socket.on('message_to_server', function (data) { 
      // look up a client socket by id (this would come from 
      // the client, and would need to be communicated to the 
      // client in some way, perhaps with a broadcast message 
      // sent to every client whenever another client "logged in" 
      var destination = clients[data.destinationId]; 

      // if destination is undefined (falsy) it does not 
      // exist in the hash 
      if (!destination) { 
        return; 
      } 

      // send a message to the destination 
      destination.emit("message_to_client" , { message: data["message"] }); 
    }); 
}); 

如果希望將消息發送到連接的過程中創建的同一個插座,你不」因爲你可以在關閉中訪問它:

io.sockets.on('connection', function (socket) { 

    socket.on('message_to_server', function (data) { 

      // we have a reference to socket from the closure above^
      socket.emit("message_to_client" , { message: data["message"] }); 
    }); 
}); 
+0

感謝它的工作.. – user4113195 2014-10-07 06:37:26

+0

太棒了。您是否願意將我的答案標記爲解決方案,以便其他人也可以從中受益?謝謝! – 2014-10-07 14:41:54

相關問題