2016-08-17 60 views
1

服務器 -使用Socket.io與多個客戶端連接到同一臺服務器

var dgram = require('dgram'); 
    var client= dgram.createSocket('udp4'); 

    /** @requires socket.io */ 
    var io = require('socket.io')(http); 

    /** Array of clients created to keep track of who is listening to what*/ 
     var clients = []; 

     io.sockets.on('connection', function(socket, username){ 
     /** printing out the client who joined */ 
     console.log('New client connected (id=' + socket.id + ').'); 

     /** pushing new client to client array*/ 
     clients.push(socket); 



     /** listening for acknowledgement message */ 
     client.on('message', function(message, rinfo){ 
     /** creating temp array to put data in */ 
     var temp = []; 
     /**converting data bit to bytes */ 
     var number= req.body.size * 2 
     /** acknowledgement message is converted to a string from buffer */ 
     var message = message.toString(); 
     /** cutting hex string to correspong to requested data size*/ 
     var data = message.substring(0, number); 
     /** converting that data to decimal */ 
     var data = parseInt(data, 16); 
     /** adding data to data array */ 
     temp[0] = data 
     /** emitting message to html page */ 
     socket.emit('temp', temp); 
     }); 

     /** listening if client has disconnected */ 
     socket.on('disconnect', function() { 
      clients.splice(clients.indexOf(client), 1); 
      console.log('client disconnected (id=' + socket.id + ').'); 
      clearInterval(loop); 
     }); 
    }); 
    } 
}); 

客戶 -

var socket = io.connect('192.168.0.136:3000'); 



    socket.on(temp', function(temp){ 
     var temp= temp.toString(); 
    var message= temp.split(',').join("<br>"); 
    $('#output').html('<output>' + message + '</output>'); 
    }); 

當客戶端連接,一個叫做臨時隨機數被髮射到客戶端。上面的代碼在一個客戶端連接到服務器時起作用。現在,你怎麼能每次設置一個新的連接?因此,如果打開一個選項卡,它將獲得自己的隨機消息,而當另一個選項卡打開時,它將獲得自己的隨機消息。

+0

你可以發送一個ID返回給客戶端,並將其保存到localStorage的(或變量或其他地方)。然後讓客戶端聽一個僅供此客戶使用的「房間」。例如'var uuidSocket = io(serverHost +「/」+ uuid); uuidSocket.on(「Info」,(data:any)=> {//對數據做某事 });'這個id/room將被服務器用來通知特定的客戶端。等等。 – mkaran

+0

你可以給我一個更好的例子嗎? – TheFlight

+0

我已經添加了答案,我希望它有幫助! – mkaran

回答

0

你可以嘗試這樣的事: 服務器端:

// you have your socket ready and inside the on('connect'...) you handle a register event where the client passes an id if one exists else you create one. 

socket.on('register', function(clientUuid){ // a client requests registration 
     var id = clientUuid == null? uuid.v4() : clientUuid; // create an id if client doesn't already have one 
     var nsp; 
     var ns = "/" + id; 

     socket.join(id); 
     var nsp = app.io.of(ns); // create a room using this id that is only for this client 
     clientToRooms[ns] = nsp; // save it to a dictionary for future use 

     // set up what to do on connection 
     nsp.on('connection', function(nsSocket){ 
     console.log('someone connected'); 

     nsSocket.on('Info', function(data){ 
      // just an example 
     }); 
     }); 

客戶端:

// you already have declared uuid, uuidSocket and have connected to the socket previously so you define what to do on register: 
    socket.on("register", function(data){ 
     if (uuid == undefined || uuidSocket == undefined) {// first time we get id from server 
     //save id to a variable 
     uuid = data.uuid; 

     // save to localstorage for further usage (optional - only if you want one client per browser e.g.) 
     localStorage.setItem('socketUUID', uuid); 

     uuidSocket = io(serverHost + "/" + uuid); // set up the room --> will trigger nsp.on('connect',...) on the server 

     uuidSocket.on("Info", function(data){ 
      //handle on Info 
     }); 

// initiate the register from the client 
socket.emit("register", uuid); 
相關問題