Socket.io连接恢复为轮询,从不触发'连接'处理程序

时间:2022-08-22 14:37:16

I'm trying to add socket.io to my existing node.js app on express. I've added the socket.io library in the server-side as follows (directly following http://socket.io/get-started/chat/):

我正在尝试将socket.io添加到express上现有的node.js应用程序中。我已经在服务器端添加了socket.io库,如下所示(直接在http://socket.io/get-started/chat/之后):

var express = require('express')
    , http = require('http')
    , path = require('path')
    , fs = require('fs');

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

// Express settings [...]
// Express routes [...]

// Socket.io Communication
io.on('connection', function(socket) {
  console.log('a user connected');
});


// Start server
app.listen(config.port, function () {
  console.log('Express server listening on port %d in %s mode', config.port, app.get('env'));
});

Right now, on the front-end I am simply making a connection:

现在,在前端我只是建立一个连接:

<script src="/socket.io/socket.io.js"></script>
<script>
  var io = io();
</script>

But instead of showing "a user connected" in the console, the console logs a continuous stream of polls. I am using the latest version of Chrome on Mac, which supports websockets.

但是,控制台不是在控制台中显示“用户连接”,而是记录连续的轮询流。我在Mac上使用最新版本的Chrome,它支持websockets。

GET /socket.io/?EIO=2&transport=polling&t=1402521519446-91 200 94ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519447-92 200 93ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519485-93 200 53ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519580-94 200 143ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519582-95 200 144ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519633-96 200 40ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519778-97 200 92ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519780-98 200 92ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519818-99 200 36ms - 6.96kb
GET /socket.io/?EIO=2&transport=polling&t=1402521519912-100 200 81ms - 6.96kb
[etc]

I must be doing something wrong. I'm pretty new to this, and I'd love to be pointed in the right direction. Let me know if I need to elaborate on any part of this question.

我一定做错了什么。我对此很陌生,我很乐意指出正确的方向。如果我需要详细说明这个问题的任何部分,请告诉我。

Thanks! - Edward

谢谢! - 爱德华

===========

===========

EDIT:

Here's the express settings I'm currently using. I tried the same steps on a completely new node app and it seemed to work fine, so I'm wondering if any of this might be the issue.

这是我目前正在使用的快速设置。我在一个全新的节点应用程序上尝试了相同的步骤,它似乎工作正常,所以我想知道是否有任何这可能是问题。

app.configure('development', function(){
    app.use(require('connect-livereload')());

    // Disable caching of scripts for easier testing
    app.use(function noCache(req, res, next) {
        if (req.url.indexOf('/scripts/') === 0) {
            res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
            res.header('Pragma', 'no-cache');
            res.header('Expires', 0);
        }
        next();
    });

    app.use(express.bodyParser({limit: '50mb'})); // increase limit for audio recordings
    app.use(express.static(path.join(config.root, '.tmp')));
    app.use(express.static(path.join(config.root, 'app')));
    app.use(express.errorHandler());
    app.use(express.logger('dev'));

    util.logger.add(loggly, { 
        [...Credentials...]
    });
    app.set('views', config.root + '/app/views');
});

2 个解决方案

#1


28  

I had the same problem and the way how I solved it was by replacing this

我有同样的问题,我解决它的方式是替换它

// Start server
app.listen(config.port, function () {
    console.log('Express server listening on port %d in %s mode', config.port, app.get('env'));
});

with this:

有了这个:

// Start server
http.listen(config.port, function () {
    console.log('Express server listening on port %d in %s mode', config.port, app.get('env'));
});

This post kinda explains why: https://*.com/a/17697134/1515130

这篇文章有点解释了原因:https://*.com/a/17697134/1515130

#2


-8  

I don't know, if that's your problem, but my eye stuck on your html script tag, with that source path of the js.

我不知道,如果这是你的问题,但我的目光停留在你的html脚本标签上,带有js的源路径。

<script src="/socket.io/socket.io.js"></script>

I think the folder handling is not right. It should be:

我认为文件夹处理不对。它应该是:

<script src="./socket.io/socket.io.js"></script>

Or maybe:

或者可能:

<script src="socket.io/socket.io.js"></script>

Because in my opinion the "/" at the beginning refers to the root folder. The "./" refers to the current directory and the next one, but simply the folder name and then the js inside should do the trick.

因为在我看来,开头的“/”是指根文件夹。 “./”指的是当前目录和下一个目录,但只是文件夹名称,然后内部的js应该做的伎俩。

So your node code is right, but the client side html doesn't include your script.

所以你的节点代码是正确的,但客户端html不包括你的脚本。

#1


28  

I had the same problem and the way how I solved it was by replacing this

我有同样的问题,我解决它的方式是替换它

// Start server
app.listen(config.port, function () {
    console.log('Express server listening on port %d in %s mode', config.port, app.get('env'));
});

with this:

有了这个:

// Start server
http.listen(config.port, function () {
    console.log('Express server listening on port %d in %s mode', config.port, app.get('env'));
});

This post kinda explains why: https://*.com/a/17697134/1515130

这篇文章有点解释了原因:https://*.com/a/17697134/1515130

#2


-8  

I don't know, if that's your problem, but my eye stuck on your html script tag, with that source path of the js.

我不知道,如果这是你的问题,但我的目光停留在你的html脚本标签上,带有js的源路径。

<script src="/socket.io/socket.io.js"></script>

I think the folder handling is not right. It should be:

我认为文件夹处理不对。它应该是:

<script src="./socket.io/socket.io.js"></script>

Or maybe:

或者可能:

<script src="socket.io/socket.io.js"></script>

Because in my opinion the "/" at the beginning refers to the root folder. The "./" refers to the current directory and the next one, but simply the folder name and then the js inside should do the trick.

因为在我看来,开头的“/”是指根文件夹。 “./”指的是当前目录和下一个目录,但只是文件夹名称,然后内部的js应该做的伎俩。

So your node code is right, but the client side html doesn't include your script.

所以你的节点代码是正确的,但客户端html不包括你的脚本。