小编F A*_*emy的帖子

Android 模拟器无法连接到本地主机上的 socket.io

我的js代码:

const   express = require('express'),
        http = require('http'),
        app = express(),
        server = http.createServer(app),
        io = require('socket.io').listen(server);

app.get('/', (req, res) => {
    res.send('Chat Server is running on port 3000');
});

io.on('connection', (socket) => {
    console.log('user connected');
});

server.listen(3000, ()=>{
    console.log('Node app is running on port 3000')
});
Run Code Online (Sandbox Code Playgroud)

如果我在浏览器中打开http://127.0.0.1:3000,我会收到消息

但在android中我无法连接到套接字

我的安卓代码:

try {
            Socket socket = IO.socket("http://127.0.0.1:3000");
            socket.connect();
            Log.i("errorCheck", "ID: "+socket.id());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

我测试了其他IP,例如:10.0.2.2、10.0.2.3、本地主机、调制解调器配置IP、我的真实IP

以及4444、8080端口

但不工作

我使用genymotion模拟器

sockets android socket.io

1
推荐指数
1
解决办法
3185
查看次数

标签 统计

android ×1

socket.io ×1

sockets ×1