【发布时间】:2018-12-06 11:41:45
【问题描述】:
我正在尝试从 Expo 应用程序中连接到 socket.io 服务器。我无法连接到我的服务器时遇到问题。我有一个可以很好地连接到服务器的 Web 应用程序,因此服务器工作正常。我还查看了一些示例,包括 Expo repo 中的一个示例,但均无济于事。
我的客户:
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);
});
}
服务器:
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 */}
on connect 事件永远不会触发,只有 connect_error 事件只是简单地说“超时”。在针对 https://www.websocket.org/echo.html 进行测试时,本机 websocket 可以正常工作。
【问题讨论】:
-
我听说有人对 socket.io 也有问题。启用远程调试使其对他有用。但他不知道为什么。
标签: javascript react-native socket.io expo