2

I started a project with the generator Angular-Fullstack. I want that external Node Clients (running in other machines) can connect to socket server in the Angular-FullStack project.

I have tested it with a simple node server and client and it works fine. The code is:

NODE SERVER:

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

var port = 9000;

server.listen(port, function(){
  console.log('Express server listening on %d port', port);
});

// __dirname is the directory where this file is placed
app.get('/', function (req, res) {
  res.sendFile(__dirname + '/browserSocketAppClient.html');
});

io.on('connection', function (socket) {
  console.log( 'THE SOCKET: ' + socket.toString());

  socket.emit('NODE SOCKET SERVER', { SERVER: 'NODE SOCKET SERVER' });

  socket.on('NODE SOCKET CLIENT', function (data) {
    console.log(data);
  });
});

NODE CLIENT:

var io = require('socket.io-client');

var socket = io.connect('http://192.168.0.101:9000');

socket.on('connect', function() {
  console.log( 'NODE SOCKET CLIENT has connected with the server');
});

socket.on('NODE SOCKET SERVER', function (data) {
  console.log(data);
  socket.emit('NODE SOCKET CLIENT', { CLIENT: 'NODE SOCKET CLIENT' });
});

THE SERVER CONSOLE:

D:\003_IOT\CODE_EXAMPLES\TestSocketIO>node nodeSocketServer
Express server listening on 9000 port
{ CLIENT: 'NODE SOCKET CLIENT' }

THE CLIENT CONSOLE

D:\003_IOT\CODE_EXAMPLES\TestSocketIO>node nodeSocketClient
NODE SOCKET CLIENT has connected with the server
{ SERVER: 'NODE SOCKET SERVER' }

But when I want to use the same NODE CLIENT: with the Angular-Fullstack project, it doesn't work.

I have added this code to myproject/server/config/socketio.js

socketio.js

function onConnect(socket) {

  socket.emit('NODE SOCKET SERVER', { SERVER: 'You are connected with NODE SOCKET SERVER' });
  socket.on('NODE SOCKET CLIENT', function (data) {
    console.log(data);
  });
............................
}

And I get in the console of the server this recursive message,

Express server listening on NaN : 9000 in development mode
finished populating users
GET /socket.io/?EIO=3&transport=polling&t=1438187488692-7 200 27ms
[undefined:undefined] SOCKET CONNECTED 
GET /socket.io/?EIO=3&transport=polling&t=1438187489437-306 200 9ms
GET /socket.io/?EIO=3&transport=polling&t=1438187493792-8 200 8ms
GET /socket.io/?EIO=3&transport=polling&t=1438187495438-307 200 5ms
GET /socket.io/?EIO=3&transport=polling&t=1438187498815-9 200 4ms
GET /socket.io/?EIO=3&transport=polling&t=1438187501438-308 200 5ms
GET /socket.io/?EIO=3&transport=polling&t=1438187503827-10 200 7ms

But it never really connects. It has to be something in the configuration in the server side of the Angular-Fullstack project, but I can't fid it.

Any Idea Thanks

  • Seems like the client can't reach the socket.io client-side code. Can you test if the client has the correct client? (Check that in the developer's console o firebug) – Kangcor Jul 30 '15 at 09:16
  • When i debug the sodeSocketClient.js it finds the 'socket.io-client' library because "io" gets populated. But after pass over "var socket = io.connect('http://localhost:9000');" the parameters of "socket" are connected = false and disconnected = true. So it can't connect for some reason. – Toni Benitez Jul 30 '15 at 09:59

0 Answers0