javascript Socket.io 无法连接,求助于“轮询”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27998407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:16:19  来源:igfitidea点击:

Socket.io cannot connect, resorts to "polling"

javascriptangularjsnode.jssocketssocket.io

提问by Tom S?derlund

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

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

Setup

设置

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

Client and server

客户端和服务器

Server

服务器

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

客户

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

Results

结果

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

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

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

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

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

Any ideas?

有任何想法吗?

Update

更新

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

这是整个客户端/服务器项目,在 README 中有说明:https: //github.com/weld-io/socket.io-client-server-boilerplate

回答by leszek.hanusz

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

server/app.js 中的路径定义不正确

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

尝试像这样使用“/socket.io”路径:

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

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

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

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

回答by John Hua

I suggest you test client-server respectively. nc will be an ideal alternative for this kind of situation.

我建议你分别测试客户端 - 服务器。nc 将是这种情况的理想选择。

  1. start server and echo testserver | nc server_ip server_portand check server log.

  2. start client and netcat -l -p server_portand check client log.

  1. 启动服务器并echo testserver | nc server_ip server_port检查服务器日志。

  2. 启动客户端并netcat -l -p server_port检查客户端日志。

BTW, the client code you've showed doesn't seem to attempt to connect to the server.

顺便说一句,您展示的客户端代码似乎并未尝试连接到服务器。

回答by Jean-Baptiste Louazel

I think it's because your server are using sockets on http://localhost:9007and your client on http://localhost:9006. Try to put both on the same port, it should work.

我认为这是因为您的服务器在http://localhost:9007上使用套接字,而您的客户端在http://localhost:9006 上使用。尝试将两者放在同一个端口上,它应该可以工作。

回答by ahmed.hoban

Could try 2 things: first change

可以尝试两件事:首先改变

window.io.connect('http://... 

to

window.io.connect('ws://...

Secondly on server side set

其次在服务器端设置

socketio.set('transports',['xhr-polling']);

I know that looks incorrect, since you want to avoid polling, still it helped a lot of people who had the same problem.

我知道这看起来不正确,因为你想避免投票,但它仍然帮助了很多有同样问题的人。