【发布时间】:2021-11-11 10:04:25
【问题描述】:
我在尝试在 android 客户端和运行 Node.js 的本地套接字服务器之间建立初始连接时收到错误消息,但一直收到此 xhr 轮询错误,过去几个小时我一直在尝试解决此问题,但是运气不好,任何帮助将不胜感激。我已将正确的权限添加到清单中,并确保使用服务器(本地计算机)的私有 IP,我可以浏览 / telnet 到此端口,我在模拟器和物理设备上都尝试过,但收到相同错误。
客户端连接
@Synchronized
fun setSocket() {
try {
Log.d("socketHandler", "setting Socket" )
mSocket = IO.socket("http://192.168.1.156:3000")
mSocket.on(Socket.EVENT_CONNECT_ERROR){
Log.d("SocketHandler" ,"Connection Error : ${it}")
it.forEach { item ->
val exception = item as EngineIOException
print(exception.message + " " + exception.code + " " + exception.cause)
}
}
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT){
Log.d("SocketHandler", "TimeOut Error : $it")
}
establishConnection()
} catch (e: Exception) {
when(e){
is URISyntaxException -> e.printStackTrace()
is EngineIOException -> {
Log.d("EngineException", "${e.code}, ${e.transport}, ${e.message}")
e.printStackTrace()
}
else -> e.printStackTrace()
}
}
}
主目录
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.AirHockeyCompose">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.AirHockeyCompose.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
服务器
const { Server } = require("socket.io");
const io = new Server({ /* options */ });
io.on("connection", (socket) => {
console.log("connection")
console.log(socket)
});
io.listen(3000);
错误(在 Socket.EVENT_CONNECT_ERROR 中收到)
xhr poll error null java.io.IOException: 400io.socket.engineio.client.EngineIOException: xhr poll error
【问题讨论】:
标签: android node.js kotlin socket.io