Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io binding to a specific ip address

I have a windows server running IIS and node.js with socket.io for an interactive whiteboard application I am developing. I want both the IIS website and node.js server to listen on port 80, but be bound to different IP addresses.

From what I can find, socket.io doesn't have the ability to specify ip address. This limitation can supposedly be overcome by creating an http server instance. I'm new to socket.io and node.js and am a little lost trying to do this. I'll include the original server code (that listens on a specific port) and my attempt to instantiate a server on a specific IP address.

Original

(function() {
  var io;
  io = require('socket.io').listen(80);
  io.sockets.on('connection', function(socket) {
    socket.on('drawClick', function(data) {
      socket.broadcast.emit('draw', {
        x: data.x,
        y: data.y,
        type: data.type
      });
    });
  });
}).call(this);

Modified

(function() {
    var host = "10.70.254.76";
var port = 80;

var http = require("http").createServer();
var io = require("socket.io").listen(http);

http.listen(port, host);
io.sockets.on('connection', function(socket) {
    socket.on('drawClick', function(data) {
        socket.broadcast.emit('draw', {
            x: data.x,
            y: data.y,
            type: data.type
        });
    });
});
}).call(this);

I get Error: listen EACCES when starting the modified code

like image 537
TylerEKnight Avatar asked Feb 23 '12 18:02

TylerEKnight


1 Answers

i see nobody answered, so i'll post this, not sure if it'll help you:

var io = require('socket.io');
var server = http.createServer();
server.listen(8080, 'localhost');
var socket = io.listen(server);

basically just switch the calls to listen.

like image 92
mindandmedia Avatar answered Sep 18 '22 23:09

mindandmedia