websocket - Using Socket.io to emit and receive messages on the client -
so using socket.io send messages , forth.
i use socket raise events , catch them within client itself. far, have tried (in chrome console i.e. client):
socket.on('news', function(data) { console.log(data); });
but, when ran following (from chrome console):
socket.emit('news', {my: 'data'});
i did not console.log message.
could please me achieve desired result ?
the code below:
socket.on('news', function(data) { console.log(data); });
runs in server application should see them in cmd
(windows) instead of browser. safe hard code socket.emit('news', {my: 'data'});
in client application/script if can't still see log in terminal. socket.io might not available in window
if way.
if want raise event , catch them within client, can this:
server:
socket.on('news', function(data){ var msg = data + 'world'; socket.emit('news-response', msg); });
client:
socket.emit('news', 'hello'); socket.on('news-response', function(data){ console.log(data); //should output 'hello world' });
Comments
Post a Comment