本文介绍了Socket.io连接恢复为轮询,从不触发“连接"处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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/):

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>

但是,控制台没有在控制台中显示用户已连接",而是记录了连续的民意测验流.我正在Mac上使用支持Websocket的最新版本的Chrome.

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.

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.

谢谢! -爱德华

===========

===========

这是我当前正在使用的快速设置.我在一个全新的节点应用程序上尝试了相同的步骤,它似乎运行良好,所以我想知道是否有任何问题.

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');
});

推荐答案

我遇到了同样的问题,而解决问题的方法是替换掉

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'));
});

与此:

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

这篇文章有点解释了为什么: https://stackoverflow.com/a/17697134/1515130

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

这篇关于Socket.io连接恢复为轮询,从不触发“连接"处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:23