WestleyArgentum
WestleyArgentum

Reputation: 2187

Connecting an application to a node server using socket.io

I'm trying to connect a client side application (written in c++, although that shouldn't have any impact) to a node.js server that is written using express and socket.io.

The server pretty much boils down to:

var express = require('express');
var http = require('http')

var app = express();
var server = http.createServer(app)
var io = require('socket.io').listen(server);

app.use(express.cookieParser());
app.use(express.session({
  ...
}));
app.use(app.router);

// response handling -------
app.get('/', function(req, res){
  console.log('request get /');
});

io.sockets.on('connection', function(socket) {
  console.log('sockets.io connection!');
});
// -------

port = 3000
app.listen(port);
console.log('Listening on port: ' + port);

When I fire up this node I try two things:

  1. I try to connect with a browser. This works in the sense that it will trigger the apt.get('/' ...), but it does not trigger io.sockets.on('connection' ...) which is unexpected and makes me suspicious that I've misunderstood something.
  2. I start my c++ application and call connect on a tcp socket. The socket connects and lets me send data but I never get any indication that something has happened on the node side (and I never get any data back). io.sockets.on('connection' ...) is never fired and no errors about malformed messages come up.

So my question is: have I misunderstood either the setup or function of sockets.io? How can I connect my application to this node server?

Thanks!

Upvotes: 1

Views: 2732

Answers (1)

19h
19h

Reputation: 829

Firstly, do you include the module-specific socket.io library on the client? Do you connect to the right socket / end-point?

io.connect('http://localhost');

For the native application, you can try using already implemented Socket.IO-modules for C++.

The latter one uses Boost.


If I understood something wrong, please post some debug info by dumping the internal state on a new connection-trial.

Upvotes: 1

Related Questions