Socket.IO服务端无法接收Android Studio客户端连接问题求助
Node.js Socket.IO 4.7.2 与 Android Socket.IO 2.0.0 连接失败排查
我在搭建Node.js Socket.IO(4.7.2版本)服务端和Android Studio(Socket.IO 2.0.0版本)客户端连接时遇到问题,启动Activity后服务端完全没有反应,无法检测到客户端连接。
服务端代码
const express = require('express'); //requires express module const socket = require('socket.io'); //requires socket.io module const fs = require('fs'); const app = express(); var PORT = process.env.PORT || 3000; const server = app.listen(PORT); //tells to host server on localhost:3000 //Playing variables: app.use(express.static('public')); //show static files in 'public' directory console.log('Server is running'); const io = socket(server); //Socket.io Connection------------------ io.on('connection', function(socket) { console.log('Client connected.'); // Disconnect listener socket.on('disconnect', function() { console.log('Client disconnected.'); }); });
Android客户端 SocketHandler.kt 代码
import io.socket.client.IO import io.socket.client.Socket import java.net.URISyntaxException object SocketHandler { lateinit var mSocket: Socket @Synchronized fun setSocket() { try { // "http://10.0.2.2:3000" is the network your Android emulator must use to join the localhost network on your computer // "http://localhost:3000/" will not work // If you want to use your physical phone you could use your ip address plus :3000 // This will allow your Android Emulator and physical device at your home to connect to the server mSocket = IO.socket("http://0.0.0.0:3000") } catch (e: URISyntaxException) { } } @Synchronized fun getSocket(): Socket { return mSocket } @Synchronized fun establishConnection() { mSocket.connect() } @Synchronized fun closeConnection() { mSocket.disconnect() } }
Android客户端 MainActivity 代码
import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) SocketHandler.setSocket() val mSocket = SocketHandler.getSocket() mSocket.connect() val Dec_button = findViewById<TextView>(R.id.login_button) Dec_button.setOnClickListener { val intent = Intent(this, LoginPage::class.java) startActivity(intent) } } }
已尝试的操作:
- 重启服务端
- 更换本地IP地址
- 搜索相关资料
排查与修复步骤
- 版本兼容性适配
Socket.IO 4.x服务端默认使用v4协议,而2.x客户端仅支持v3及以下协议,需在服务端启用兼容配置:
const io = socket(server, { cors: { origin: "*", methods: ["GET", "POST"] }, allowEIO3: true // 开启对旧版本协议的兼容 });
- 修正客户端连接地址
- Android模拟器需使用
http://10.0.2.2:3000(模拟器映射电脑本地回环的专用IP) - 物理设备需使用电脑局域网IP(如
http://192.168.x.x:3000),且确保设备与电脑在同一局域网、电脑防火墙已放行3000端口
- 添加异常日志排查
在SocketHandler的catch块中添加日志,避免忽略地址格式错误:
catch (e: URISyntaxException) { e.printStackTrace() // 打印异常信息定位问题 }
- 确认网络权限
在AndroidManifest.xml中添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
- 监听客户端连接状态
在MainActivity中添加连接事件监听,排查错误原因:
mSocket.on(Socket.EVENT_CONNECT) { runOnUiThread { // 连接成功回调 } } mSocket.on(Socket.EVENT_CONNECT_ERROR) { args -> runOnUiThread { val errorMsg = args[0].toString() // 打印连接错误信息 } }
内容的提问来源于stack exchange,提问作者ArtDigital
相关产品推荐
相关产品推荐

