validar validacion vacios pagina funcion formularios formulario enviar ejemplos ejecutar con cargar carga campos asincrona antes javascript node.js socket.io

validacion - validar formulario javascript html5



Enviar respuesta a todos los clientes excepto remitente (10)

Emitir hoja de trucos

io.on(''connect'', onConnect); function onConnect(socket){ // sending to the client socket.emit(''hello'', ''can you hear me?'', 1, 2, ''abc''); // sending to all clients except sender socket.broadcast.emit(''broadcast'', ''hello friends!''); // sending to all clients in ''game'' room except sender socket.to(''game'').emit(''nice game'', "let''s play a game"); // sending to all clients in ''game1'' and/or in ''game2'' room, except sender socket.to(''game1'').to(''game2'').emit(''nice game'', "let''s play a game (too)"); // sending to all clients in ''game'' room, including sender io.in(''game'').emit(''big-announcement'', ''the game will start soon''); // sending to all clients in namespace ''myNamespace'', including sender io.of(''myNamespace'').emit(''bigger-announcement'', ''the tournament will start soon''); // sending to individual socketid (private message) socket.to(<socketid>).emit(''hey'', ''I just met you''); // sending with acknowledgement socket.emit(''question'', ''do you think so?'', function (answer) {}); // sending without compression socket.compress(false).emit(''uncompressed'', "that''s rough"); // sending a message that might be dropped if the client is not ready to receive messages socket.volatile.emit(''maybe'', ''do you really need it?''); // sending to all clients on this node (when using multiple nodes) io.local.emit(''hi'', ''my lovely babies''); };

Para enviar algo a todos los clientes, usas:

io.sockets.emit(''response'', data);

Para recibir de los clientes, usted utiliza:

socket.on(''cursor'', function(data) { ... });

¿Cómo puedo combinar los dos para que al recibir un mensaje en el servidor de un cliente, lo envío a todos los usuarios excepto a uno que envía el mensaje?

socket.on(''cursor'', function(data) { io.sockets.emit(''response'', data); });

¿Tengo que piratearlo enviando el ID del cliente con el mensaje y luego comprobando el lado del cliente o hay una forma más fácil?


Actualización de la lista para la documentación adicional.

socket.emit(''message'', "this is a test"); //sending to sender-client only socket.broadcast.emit(''message'', "this is a test"); //sending to all clients except sender socket.broadcast.to(''game'').emit(''message'', ''nice game''); //sending to all clients in ''game'' room(channel) except sender socket.to(''game'').emit(''message'', ''enjoy the game''); //sending to sender client, only if they are in ''game'' room(channel) socket.broadcast.to(socketid).emit(''message'', ''for your eyes only''); //sending to individual socketid io.emit(''message'', "this is a test"); //sending to all clients, include sender io.in(''game'').emit(''message'', ''cool game''); //sending to all clients in ''game'' room(channel), include sender io.of(''myNamespace'').emit(''message'', ''gg''); //sending to all clients in namespace ''myNamespace'', include sender socket.emit(); //send to all connected clients socket.broadcast.emit(); //send to all connected clients except the one that sent the message socket.on(); //event listener, can be called on client to execute on server io.sockets.socket(); //for emiting to specific clients io.sockets.emit(); //send to all connected clients (same as socket.emit) io.sockets.on() ; //initial connection from a client.

Espero que esto ayude.


Aquí está mi lista (actualizada para 1.0) :

// 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'');


Aquí hay una respuesta más completa sobre lo que ha cambiado de 0.9.xa 1.x.

// send to current request socket client socket.emit(''message'', "this is a test");// Hasn''t changed // sending to all clients, include sender io.sockets.emit(''message'', "this is a test"); // Old way, still compatible io.emit(''message'', ''this is a test'');// New way, works only in 1.x // sending to all clients except sender socket.broadcast.emit(''message'', "this is a test");// Hasn''t changed // sending to all clients in ''game'' room(channel) except sender socket.broadcast.to(''game'').emit(''message'', ''nice game'');// Hasn''t changed // sending to all clients in ''game'' room(channel), include sender io.sockets.in(''game'').emit(''message'', ''cool game'');// Old way, DOES NOT WORK ANYMORE io.in(''game'').emit(''message'', ''cool game'');// New way io.to(''game'').emit(''message'', ''cool game'');// New way, "in" or "to" are the exact same: "And then simply use to or in (they are the same) when broadcasting or emitting:" from http://socket.io/docs/rooms-and-namespaces/ // sending to individual socketid, socketid is like a room io.sockets.socket(socketid).emit(''message'', ''for your eyes only'');// Old way, DOES NOT WORK ANYMORE socket.broadcast.to(socketid).emit(''message'', ''for your eyes only'');// New way

Quería editar la publicación de @soyuka pero mi edición fue rechazada por la revisión por pares.


De la respuesta @LearnRPG pero con 1.0:

// send to current request socket client socket.emit(''message'', "this is a test"); // sending to all clients, include sender io.sockets.emit(''message'', "this is a test"); //still works //or 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 // docs says "simply use to or in when broadcasting or emitting" io.in(''game'').emit(''message'', ''cool game''); // sending to individual socketid, socketid is like a room socket.broadcast.to(socketid).emit(''message'', ''for your eyes only'');

Para responder al comentario de @Crashalot, socketid viene de:

var io = require(''socket.io'')(server); io.on(''connection'', function(socket) { console.log(socket.id); })


Estoy usando espacios de nombres y habitaciones - encontré

socket.broadcast.to(''room1'').emit(''event'', ''hi'');

trabajar donde

namespace.broadcast.to(''room1'').emit(''event'', ''hi'');

No

(si alguien más enfrenta ese problema)


Otros casos

io.of(''/chat'').on(''connection'', function (socket) { //sending to all clients in ''room'' and you io.of(''/chat'').in(''room'').emit(''message'', "data"); };


Para los espacios de nombres dentro de las salas, hacer un bucle en la lista de clientes en una sala (similar a la respuesta de Nav) es uno de los dos enfoques que he encontrado que funcionarán. La otra es usar excluir. P.EJ

socket.on(''message'',function(data) { io.of( ''namespace'' ).in( data.roomID ).except( socket.id ).emit(''message'',data); }


broadcast.emit envía el msg a todos los demás clientes (excepto el remitente)

socket.on(''cursor'', function(data) { socket.broadcast.emit(''msg'', data); });


usa esta codificación

io.sockets.on(''connection'', function (socket) { socket.on(''mousemove'', function (data) { socket.broadcast.emit(''moving'', data); });

este socket.broadcast.emit () emitirá todo en la función excepto al servidor que está emitiendo