Node.js is not activated.

I am working on a simple Node.js bi-directional client \ server communication channel, and I am trying to use socket.io on the server and socket.io-client on the client.

I pasted the code below, as you will see some fairly simple material, and I am running both on my local computer - to minimize complexity.

The behavior that I see is:

  • I am starting the server.
  • In the server console, the server is running on the console.
  • I am running a client.
  • Client Log Server Ready! ''
  • Nothing else happens ...

What I expect is a server for registration "Client is ready!" and then the contents (“Finished Received”).

I even used WireShark to sniff the line, and it looks like the client is emitting a message as it was designed, but the callback on the server does not work.

I am running node v0.8.4 with express v3.1.0, socket.io v0.9.13 and socket.io-client v0.9.11 (all installed via npm).

Here is the server code ...

var http = require('http'), express = require('express'), app = express(), server = http.createServer(app); app.configure(function(){ app.use(express.static(__dirname + '/public')); }); server.listen(8080); console.log("Server started"); var io = require('socket.io').listen(server); io.sockets .on('connection', function(socket){ socket.emit('server ready', { msg: 'ready' }) ; }) .on('comms', function(content) { console.log('Client is ready!'); console.log(content); }); 

And here is the client code ...

  var clientio = require('socket.io-client'); var socket = new clientio.connect('http://localhost', { port: 8080 }); socket .on('server ready', function(data){ console.log('Server is ready!'); socket.emit('comms', 'Ready received'); }) .on('connect-error', function(error) { console.log('Connection Error\n' + error); }) .on('error', function(error) { console.log('Socket Error\n' + error); }) 

The documentation and examples for socket.io and socket.io-client are somewhat confusing (to be charitable) and they seem like a slightly moving target ... but from what I can say, I think this should work.

I hope someone can give me advice on where I am going wrong?

+6
source share
2 answers

On your server you have this code:

 io.sockets .on('connection', function(socket){ socket.emit('server ready', { msg: 'ready' }) ; }) .on('comms', function(content) { console.log('Client is ready!'); console.log(content); }); 

What you should do is something like this:

 io.sockets.on('connection', function(socket){ socket.emit('server ready', { msg: 'ready' }); socket.on('comm', function(content){ console.log('Client is ready!'); console.log(content); }); }); 
+4
source

We hope this does more or less of what you need for this. Just a few minor changes.

app.js

 var app = require('express')() , server = require('http').createServer(app) , io = require('socket.io').listen(server); // using 'connect' to handle static pages app.use(require('connect').static(__dirname + '/public')) server.listen(8080); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.sockets.on('connection', function (socket) { socket.emit('server ready', { msg: 'ready' }); socket.on('comms', function(content) { console.log(('Client is ready\n')); console.log(content); }); }); 

index.html

 <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script> <script> window.jQuery || document.write('<script src="js/jquery-1.8.3.min.js"><\/script>') </script> <script src="/socket.io/socket.io.js"></script> </head> <body> <script> (function (d, b) { function bindEvents() { function doSomething(msg) { $.each(msg, function (key, value) { console.log('doSomething...'); $("body").append("<p>" + value + "</p>") }); }; // var socket = io.connect(); var socket = io.connect('http://localhost'); socket.on('server ready', function (msg) { console.log('Server is ready!\n', msg); doSomething(msg); socket.emit('comms', { msg: 'Client is Ready' }); }); socket.on('connect-error', function (err) { console.log('Connection Error\n', err); }); socket.on('error', function (err) { console.log('Connection Error\n', err); }); }; $(document).ready(function () { bindEvents() }); })(jQuery, this) </script> 
+3
source

All Articles