node.js + socket.io + express.js:没有连接

时间:2014-01-03 12:30:29

标签: javascript node.js apache express socket.io

我一直在尝试设置一个简单的socket.io示例,但它拒绝正常工作。客户端在Apache2上运行,应该通过端口3000访问node.js-server。

似乎没有任何连接,如果服务器实际正在运行似乎并不重要 - 如果在服务器上手动停止了node.js-process({{1 }}),我的客户端控制台不会抛出任何错误。应用程序运行时会发生完全相同的结果($ kill xyz)。

服务器

info  - socket.io started

客户端

// Check dependencies & setup backend
var express = require('express');
var server = express();
var http = require('http');
var app = http.createServer(server); // somehow needed? ("Socket.IO's `listen()` method expects an `http.Server` instance as its first parameter.")
var socket = require('socket.io');
// listen @ port 3000 as definied by proxy
server.listen(3000);
// listen w/ socket.io as well
var io = socket.listen(app);

...

// socket.io
// TODO: test, added 01/03/14 
io.set('match origin protocol', true);
io.set('log level', 2);
var clients = io.sockets.clients();
io.sockets.on('connection', function(client) {
    console.log('Connection from ' + client);

    client.emit('news', {
        hello : 'world'
    });

    client.on('event', function(data) {
        console.log(data);
    });
});

编辑2:

此代码有效,但仅限本地。我甚至在服务器上安装了Apache 2.4,以确保我可以访问WebSockets(我读到了2.2.x中没有的地方)。 服务器

<script type="text/javascript" src="./js/socket.io.js" ></script>
        <script>
            try {
                var socket = io.connect('https://localhost:3000');
                console.log(io);
                console.log('check 1', socket.socket.connected); // false

                socket.on('connect', function() {
                    console.log('check 2', socket.socket.connected); // not reached
                });

                socket.on('news', function(data) {
                    console.log(data);
                });

            } catch(error) {
                console.log(error);
            } finally {
                console.log('fin');
            }
        </script>

客户端:

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

app.use(express.static(__dirname + '/'));

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

2 个答案:

答案 0 :(得分:7)

好吧,伙计们,我终于把它弄好了。

我所做的是将我的反向代理从Apache迁移到node.js / node-http-proxy-solution。我不确定这是否真的是必须的 - 但在重新配置Apache 2.2.2甚至编译Apache 2.4.7后没有帮助,我认为它值得一试。我也切换到子域而不是子目录,因为在某些情况下,子目录似乎也会导致问题。再次,不知道这只是一个预防措施还是实际上是必要的。

然而,我非常确定相关的是如何加载socket.io.js。由于我的反向代理已将请求转发到正确的(内部)IP,因此我必须通过TLD w / o端口或ws://访问socket.io.js以及实际的Websocket。

长话短说,这是相关代码:

服务器

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

app.use(express.static(__dirname + '/'));

console.log('Express server started on port %s', app);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

<强>客户端

<script src="http://mysubdomain.mydomain.com/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://mysubdomain.mydomain.com');
    // , {secure: true}

    socket.on('news', function(data) {
        console.log(data);
    });
</script>

<强>代理

var fs = require('fs'), httpProxy = require('http-proxy');

// This will crash as soon as Apache isn't running or the port isn't binded elsewhere
// TODO: fix
var proxyTable = {
    'mydomain.com/subdir' : '127.0.0.1:3000',
    'mysubdomain.mydomain.com' : '127.0.0.1:3000'
};

var httpOptions = {
    hostnameOnly : false,
    router : proxyTable
};

var httpsOptions = {
    hostnameOnly : false,
    router : proxyTable,
    https : {
        key : fs.readFileSync('/path/key', 'utf8'),
        cert : fs.readFileSync('/path/cert', 'utf8')
    }
};

httpProxy.createServer(httpOptions).listen(80); 
httpProxy.createServer(httpsOptions).listen(443);

最后但同样重要的是,对于任何通过谷歌绊倒的人来说,使用的版本是:

$ npm list --depth=0
myapp@0.0.0 /path/app
├── express@3.4.7
└── socket.io@0.9.16

$ npm list --depth=0
/path/proxy
└── http-proxy@0.10.4

答案 1 :(得分:4)

请按照以下示例进行操作:http://socket.io/#how-to-use

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

此外这是错误的:

io.connect('https://localhost:3000');

应为ws://localhost:3000/