javascript - Socket.io (1.x) .to(room) not working, but works without -


I am creating a simple messaging service and it is socket.broadcast.emit () However, if you use socket.broadcast.in (room) .emit () then I have included the server code and the client code below it does not connect.

The server

  requires the app = ('Express') (); Var http = Required ('http'). Server (app); Var io = Requirement ('socket.io') (HT); Var pars = required ('pars'). Pars; Var room; App.get ('/', function (rik, ridge) {res.sendfile ('index.html');}); // When a new connection is started io.on ('connection', function (socket) {console.log ('user associated:' + socket.id); // ========= = INIT connection ========= Once a customer is connected, then we expect them to get a ping which is saying what they want to be in the socket in the room. ('Room', function (data) {room = data; 'socket.join (room); console.log (' + user: '+ socket.id +' - joined the cell: ['+ room + ']'); Console.log ('+' Ta: '+ socket.id +' - Rooms are: ['socket.rooms +'] '); // Display all sockets connected in the room (room);}); // ====== == Representative message ======== Socket.on ('message_send', function (data) {var JSONdata = JSON.parse (data); var room_l = JSONdata.chat_id; var msg = JSONdata.message; console .log ('room:' + room_l + '- message:' msg); // send message //io.emit('message_received ', msg); //socket.broadcast.emit('message_received', msg); Io.in ('TZRfM7V5HH'). Emit ('Message_recieved', msg); / /io.to(room_l).emit('message_received ', msg); // Save the message to parse SAVE (data); }); });  

Customer

  & lt; Script & gt; Var Socket = Io (); Var msg = $ ('# m'). Val (); Var room = 'TJRFM 7V5HH'; $ (Document) .ready (function () {socket.emit ('room', room); socket.emit ('message_send', '{"chat_id": "' + room + '", "message": " Yoyoyo shits to promote "} ');}); Val (); Socket.emit ('message_send', '{"chat_id": "' + + room + '", "$" ("# msg"). $ (' # M '). $ (' & Lt; li & gt; ; '). Text ($ msg)); $ ('# M'). Val (''); return false; }); Socket.on ('message_received', function (msg) {$ ('# messages'). Append ($ (' 

I am playing with this week and I think what is wrong with my implementation, any help or hint will be appreciated!


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -