简体   繁体   English

socket.io绑定到特定的ip地址

[英]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. 我有一个运行IIS的Windows服务器和带有socket.io的node.js,用于我正在开发的交互式白板应用程序。 I want both the IIS website and node.js server to listen on port 80, but be bound to different IP addresses. 我希望IIS网站和node.js服务器都在端口80上侦听,但绑定到不同的IP地址。

From what I can find, socket.io doesn't have the ability to specify ip address. 根据我的发现,socket.io无法指定IP地址。 This limitation can supposedly be overcome by creating an http server instance. 可以通过创建http服务器实例来克服此限制。 I'm new to socket.io and node.js and am a little lost trying to do this. 我是socket.io和node.js的新手,尝试这样做有点失落。 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. 我将包括原始服务器代码(侦听特定端口)以及我尝试在特定IP地址上实例化服务器。

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 我得到错误:在启动修改后的代码时听EACCES

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. 基本上只是将呼叫切换为监听。

I got it! 我知道了!

The problem actually lies with IIS and it's socket pooling behavior. 问题实际上在于IIS和它的套接字池行为。 IIS automatically reserves port 80 for all IP addresses (even if they are not currently bound to a site). IIS自动为所有IP地址保留端口80(即使它们当前未绑定到站点)。 What I had to do was use the netsh command to override the default bindings and bind ip addresses via the netsh command. 我要做的是使用netsh命令覆盖默认绑定并通过netsh命令绑定IP地址。

Here's what I did. 这就是我做的。 This solution works for IIS 7 in Windows Server 2008 R2. 此解决方案适用于Windows Server 2008 R2中的IIS 7。

netsh http add iplisten ipaddress x.x.x.x
net stop http
net start http
net start w3svc

In my case I only have one active website being served by IIS and only had one IP bound to it. 在我的情况下,我只有一个由IIS提供服务的活动网站,并且只有一个IP绑定到它。 This freed up my secondary IP address for node to listen on it and port 80. 这释放了我的辅助IP地址,节点可以监听它和端口80。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM