2016-02-28 11 views
12

Sto usando Socket IO v1.4.5 e ho provato 3 modi diversi di seguito ma non ho alcun risultato.Invio messaggio a client specifici in Socket IO

client.emit('test', 'hahahaha'); 
io.sockets.socket(id).emit('test',''hahaha); 
io.sockets.connected[id].emit('test','hahaha'); 

Ecco il mio lato server

var socket = require('socket.io'); 
var express = require('express'); 
var http = require('http'); 
var dateFormat = require('date-format'); 
var app = express(); 
var server = http.createServer(app); 
var io = socket.listen(server); 
io.sockets.on('connection', function(client) { 
    user[client.id]=client; 

//when we receive message 
    client.on('message', function(data) { 
     console.log('Message received from' + data.name + ":" + data.message +' avatar' +data.avatar); 
     client.emit('test', 'hahahaha'); 
}); 

Qualsiasi aiuto sarebbe great.Thanks per help.Kind Regard

risposta

70

Per inviare un messaggio a un client specifico è necessario farlo in questo modo :

socket.broadcast.to(socketid).emit('message', 'for your eyes only'); 

Ecco un bel foglio di piccolo trucco per prese:

// sending to sender-client only 
socket.emit('message', "this is a test"); 

// sending to all clients, include sender 
io.emit('message', "this is a test"); 

// sending to all clients except sender 
socket.broadcast.emit('message', "this is a test"); 

// sending to all clients in 'game' room(channel) except sender 
socket.broadcast.to('game').emit('message', 'nice game'); 

// sending to all clients in 'game' room(channel), include sender 
io.in('game').emit('message', 'cool game'); 

// sending to sender client, only if they are in 'game' room(channel) 
socket.to('game').emit('message', 'enjoy the game'); 

// sending to all clients in namespace 'myNamespace', include sender 
io.of('myNamespace').emit('message', 'gg'); 

// sending to individual socketid 
socket.broadcast.to(socketid).emit('message', 'for your eyes only'); 

credito al https://stackoverflow.com/a/10099325


Il modo più semplice invece di inviare direttamente alla presa, sarebbe la creazione di una camera per le 2 utenti di utilizzare e solo inviare liberamente messaggi in là.

socket.join('some-unique-room-name'); // Do this for both users you want to chat with each other 
socket.broadcast.to('the-unique-room-name').emit('message', 'blah'); // Send a message to the chat room. 

In caso contrario, si sta andando ad avere bisogno di tenere traccia di ogni singolo cliente connessione socket, e quando si desidera chattare dovrete guardare in alto che prese di collegamento ed emettono specificatamente a quello usando la funzione Ho detto sopra. Le camere sono probabilmente più facili.

+0

Hi.So ho solo bisogno di aggiungere che la linea per il sito del server? E qualcos'altro sembra buono? –

+1

@ HoàngPhúcVũ Non sono sicuro al 100% di ciò che stai cercando di ottenere nel tuo codice, dici di voler inviare a una specifica connessione socket, ma non vedo alcun tentativo di cercare un ID socket e di inviarlo a loro. – Datsik

+0

Quello che voglio è inviare un messaggio privato tra 2 user.So avete qualche suggerimento su questo case.Grazie per aiuto –

0

Socket.io versione 2.0.3+

L'invio di un messaggio ad un socket specifico

let namespace = null; 
    let ns = _io.of(namespace || "/"); 
    let socket = ns.connected[socketId] // assuming you have id of the socket 
    if (socket) { 
     console.log("Socket Connected, sent through socket"); 
     socket.emit("chatMessage", data); 
    } else { 
     console.log("Socket not connected, sending through push notification"); 
    }