Socket.io无法连接,只能进行“轮询". [英] Socket.io cannot connect, resorts to "polling"

查看:505
本文介绍了Socket.io无法连接,只能进行“轮询".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个websocket客户端-服务器应用程序,其中客户端和服务器将在两个不同的实例上运行.

I'm trying to create a websocket client-server app where client and server will run on two different instances.

  • 服务器/后端:使用 angular-fullstack generator localhost:9006上运行包括socket.io
  • 客户端/前端:使用角度生成器 + socket.io-clientlocalhost:9007上运行+ btford.socket-io(一个AngularJS socket.io桥)
  • Server/Back-end: running on localhost:9006 with angular-fullstack generator including socket.io
  • Client/Front-end: running on localhost:9007 with angular generator + socket.io-client + btford.socket-io (a AngularJS socket.io bridge)

注意:代码不完整,但我认为是相关的.

Note: not complete code, but the pieces I think are relevant.

// ----- socketio.js -----

// When the user connects.. perform this
function onConnect(socket) {
    // When the client emits 'info', this listens and executes
    socket.on('info', function (data) {
        console.info('[%s] %s', socket.address, JSON.stringify(data, null, 2));
        socket.emit('pong', 'OK!');
    });
    // Insert sockets below
    require('../api/thing/thing.socket').register(socket);
}

socketio.set('origins', 'http://localhost:9007');

// ----- express.js -----

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9007');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

// ----- app.js -----

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

客户

// ----- client app.js

angular
.module('weldCommentsClientApp', [
    'ngAnimate', 'ngAria', 'ngCookies', 'ngMessages', 'ngResource', 'ngRoute', 'ngSanitize', 'ngTouch',
    'btford.socket-io'
])
.factory('mySocket', function (socketFactory) {
    var myIoSocket = window.io.connect('http://localhost:9006');
    var mySocket = socketFactory({
        ioSocket: myIoSocket
    });
    mySocket.forward('pong');
    console.log('mySocket', mySocket);
    return mySocket;
})

// ----- client main.js

angular.module('weldCommentsClientApp').controller('MainCtrl', function ($scope, mySocket) {
    $scope.$on('socket:pong', function (ev, data) {
        console.log('socket:pong', ev, data);
    });
    mySocket.emit('info');
});

结果

在服务器或客户端上都没有控制台错误,但是它不起作用,并且服务器记录了以下100行:

Results

No console errors on server nor client, but it doesn't work and the server logs 100's of these lines:

GET /socket.io/?EIO=3&transport=polling&t=1421488528935-16027 200 2ms

...看起来像客户端通过HTTP连接,但是无法切换到websockets.

...which looks like the client connects over HTTP but fails to switch over to websockets.

有什么想法吗?

这是整个客户端/服务器项目,其中包含自述文件中的说明: https ://github.com/weld-io/socket.io-client-server-boilerplate

Here is the entire client/server project with instructions in README: https://github.com/weld-io/socket.io-client-server-boilerplate

推荐答案

在server/app.js中未正确定义路径

The path is not defined correctly in server/app.js

尝试使用"/socket.io"路径,如下所示:

Try to use '/socket.io' path like this:

var socketio = require('socket.io')(server, {
  serveClient: (config.env === 'production') ? false : true,
  path: '/socket.io'
});

接下来,要选择websocket而不是长时间轮询,可以在test-client/scripts/application.js中选择websocket传输

Next, to choose websockets instead of long-polling, you can select the websocket transport in test-client/scripts/application.js

var myIoSocket = window.io.connect('http://localhost:9006', {transports:['websocket']});

这篇关于Socket.io无法连接,只能进行“轮询".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆