繁体   English   中英

为什么Socket.io无法在我的React Native应用程序中连接

[英]Why is Socket.io not connecting in my React Native app

我正在创建一个本机应用程序,并且正在尝试将客户端应用程序连接到服务器。 我正在使用socket-io,到目前为止,我无法成功连接两者。 这是我在客户端上的代码:

import SocketIOClient from 'socket.io-client';

(...) 

if (result.type === "success") {
    console.log("Here 1");
    this.socket = SocketIOClient('http://localhost:1997');
    this.socket.emit('test', 'Hello world!');
    console.log("Here 2");
}

并且此代码输出“ Here 1”“ Here 2”

我的服务器代码如下:

let express = require('express');
let http = require('http')
let socketio = require('socket.io');

var app = express();
var server = http.Server(app);
var websocket = socketio(server);

let port = 1997;
server.listen(port, () => console.log('listening on *:' + port));

websocket.on('connection', (socket) => {
    console.log("Client joined on " + socket.id);

    websocket.on('test', function(data){
        console.log('Data Receieved: ' + data);
    });
});

此代码输出“在*:1997上侦听”,然后即使在客户端代码运行时也没有输出。 请帮助我不确定我在做什么错。

我遇到了同样的问题,将“ localhost”更改为实际的ip,对我来说就像一个魅力。

所以改变

this.socket = SocketIOClient('http://localhost:1997');

this.socket = SocketIOClient('http://1962.168.xxx.xxx:1997');

我从这里得到了答案: 将Socket.io与React Native一起使用并无法正常工作

暂无
暂无

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

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