简体   繁体   English

带有 socket.io websocket 的 Expo 无法连接

[英]Expo with socket.io websocket failing to connect

I am attempting to connect to a socket.io server from within an Expo app.我正在尝试从 Expo 应用程序连接到 socket.io 服务器。 I am having issues being able to connect to my server.我在连接到我的服务器时遇到问题。 I have a web app that connect to the server fine, so the server is working OK.我有一个可以正常连接到服务器的 Web 应用程序,因此服务器运行正常。 I have also looked at some examples, including one from the Expo repo, to no avail.我还查看了一些示例,包括来自 Expo repo 的示例,但无济于事。

My client:我的客户:

componentDidMount() {

    const socket = socketIOClient('http://192.168.1.210:80', {
      transports: ['websocket']
    });

    socket.on('connect', () => {
      console.log('Connected.');
    });

    socket.on('connect_error', (err) => {
      console.log(err);
    });
}

Server:服务器:

this.httpServer = HttpServer.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('');
});

this.httpServer.listen(80);

this.io = new IOServer({
  pingInterval: 10000,
  pingTimeout: 5000,
});

this.io.listen(this.httpServer);
this.io.set('transports', ['websocket']);

this.onConnection();

this.io.on('connection', (socket) => {/* events */}

The on connect event will never fire, only the connect_error event which simply says 'timeout'. on connect 事件永远不会触发,只会触发 connect_error 事件,它只是说“超时”。 Native websockets work fine when testing against https://www.websocket.org/echo.html .在针对https://www.websocket.org/echo.html进行测试时,本机 websockets 工作正常。

This was a bug with socket.io to do with loading in the incorrect WebSocket implementation.这是 socket.io 的一个错误,与加载不正确的WebSocket实现有关。

It was looking for self.WebSocket , but self does not exist in React Native, so it was falling back to require('ws') , the Node.js implementation.它正在寻找self.WebSocket ,但self在 React Native 中不存在,因此它退回到require('ws') ,即 Node.js 实现。 In reality, it should have been using the native WebSocket implementation in React Native.实际上,它应该一直使用 React Native 中的原生WebSocket实现。

I've made a PR to solve this: https://github.com/socketio/engine.io-client/pull/607我做了一个 PR 来解决这个问题: https ://github.com/socketio/engine.io-client/pull/607

The reason that turning on remote debugging makes it work is since the code runs in a browser (Chrome) in this mode, it's able to use the browser's WebSocket implementation.打开远程调试使其工作的原因是因为代码以这种模式在浏览器 (Chrome) 中运行,它能够使用浏览器的WebSocket实现。

is it related to the https issue?, tried on localhost works on https, but http, seems the https://github.com/expo/examples/tree/master/with-socket-io mentioned something about 是否与https问题有关?,在localhost上尝试过,可在https上使用,但http似乎https://github.com/expo/examples/tree/master/with-socket-io提到了一些有关

React Native provides a socket-io compatible WebSocket implementation, some people get tripped up on the https requirement so this example helps to clarify how you can get it running. React Native提供了与Socket-io兼容的WebSocket实现,有些人不满意https要求,因此此示例有助于阐明如何使其运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM