Socket.io Handshake在站点上失败,但在localhost上失败

时间:2014-11-18 11:39:37

标签: socket.io

Socket.io握手在我的网站上失败但在localhost上失败。 我不得不提供自定义握手,因为socket.io无法找到我的查询参数。

这是我在socket.service.js中的声明:

var ioSocket = io('https://my-site.rhcloud.com//?EIO=2&transport=polling&t=1414328625757-0&
token=' + Auth.getToken(), {

});

以及如何在服务器端捕获它:

socketio.use(function(socket, next) {
      request.get('https://my-site.rhcloud.com/api/users/chan', {qs:{access_token:socket.handshake.query.token}}, function(err, me) {
          if (err||!me.body|| me.body=='Unauthorized') {
              if (!me) console.log('!me');
              if (err) console.log(err);
              next(err);
          }
          else {
              // perfoming operations
              next();
          }
      });
  });

以下是我收到的消息:

WebSocket connection to 'wss://my-site.rhcloud.com/socket.io/?EIO=2&transport=websocket&t=…YwMX0.1F6ebfNxzoDPYffXGapGMzLFPJd-mfN0EexqZzXXo7A&sid=z0Jmrbgb7OS0nbqxAAAG' failed: Error during WebSocket handshake: Unexpected response code: 400 

我真的迷失在这里,并在没有任何成功的情况下深入挖掘了Google。 任何帮助都会非常感激!

3 个答案:

答案 0 :(得分:12)

经过大量搜索,我发现问题来自Openshift。您必须指定要使用的端口套接字。请参阅此文章:https://blog.openshift.com/paas-websockets/

所以,我只需要写:

var ioSocket = io('http://my-site.rhcloud.com:8000, {

});

答案 1 :(得分:8)

Nginx代理使用以下配置解决了该问题:

location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}

原始解决方案here。它对我很有用(Ubuntu 14.4 + Plesk 12.5)。

答案 2 :(得分:4)

我将节点/快速服务器部署到Azure,我收到了以下错误

websocket.js:100个WebSocket连接到'ws://mywebsitename.azurewebsites.net/socket.io/?EIO = 3& transport = websocket& sid = ssEHs-OWxI6mcChyAAAB'失败:WebSocket握手期间出错:意外的响应代码:503

,我通过在Azure控制台中启用WebSocket来修复我的问题,如下所示 https://www.algolia.com/doc/api-client/python/search#search-in-an-index

相关问题