【发布时间】:2023-03-05 04:51:01
【问题描述】:
所以在这里,我正在尝试使用 socket.io 将我的 android 连接到我的服务器 node.js,但在执行过程中,没有错误但连接尚未建立并返回 false
提前感谢您的帮助!
Socket.Java
package com.example.myprojectapplication.ObjectClass;
import com.github.nkzawa.socketio.client.IO;
import java.net.URISyntaxException;
public class Socket {
private com.github.nkzawa.socketio.client.Socket mSocket;
{
try {
mSocket = IO.socket("localhost:8081/");
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
public com.github.nkzawa.socketio.client.Socket getSocket() {
return mSocket;
}
}
我的 MainActivity 的相关部分
com.example.myprojectapplication.ObjectClass.Socket app = new com.example.myprojectapplication.ObjectClass.Socket();
Socket mSocket = app.getSocket();
mSocket.connect();
Node.js 服务器
var mysql = require('mysql');
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.emit('message', 'Android Connected');
console.log("Android Connected");
socket.on('message', function(message){
console.log("message received");
socket.emit('message', 'message received');
});
});
server.listen(8081)
【问题讨论】:
标签: java android node.js android-studio socket.io