socket.io - Calling socket.emit('init', 'data') from client side javascript function does not execute. -


this question has answer here:

on client side:

<a href='javascript:callinit();'> init </a> 

client side html includes following script above:

<script src="/socket.io/socket.io.js"></script> <script>var socket = io.connect('http://localhost:3000');  function callinit(){   alert('inside callinit');   socket.emit('init', 'data'); } </script> 

on server-side:

 io.sockets.on('connection', function(socket) { console.log('socket connection established');     socket.on('init', function(data){     console.log('init invoked');     init();     });  }); 

in above code: alert('inside callinit') executes. console.log('socket connection established'); executes. console.log('init invoked);` not execute.

that because io.connection(...) async , socket not connected @ time callinit function called.

you might want add on('connect', ....) browser side socket connection , on('error', ...) check connections , errors


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -