【问题标题】:Trying to connect to server using Socket.IO on Android receives 400 error xhr poll error尝试在 Android 上使用 Socket.IO 连接到服务器收到 400 错误 xhr poll 错误
【发布时间】: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


    【解决方案1】:

    我不是这方面的专家,但我在某次任务中做过。我拥有的是一个 nodejs 服务器,其中包含一些图像和一个通过套接字连接到它以显示它们的 android。

    连接正常,我没有任何错误,如果对您有帮助,您可以在此处随意检查https://github.com/aliosman21/Minly-Task。

    至于你给定的 sn-ps,我唯一注意到的是模拟器上的服务器 IP 应该是 10.0.2.2(如果有内存的话),但由于你已经在物理设备上尝试过,所以我不太确定。

    我相信我们都使用了相同的客户端库https://socketio.github.io/socket.io-client-java/initialization.html

    要确保的另一件事是,如果您在服务器上使用自签名证书,则必须在选项中传递 {rejectUnauthorized: false}

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 2017-12-14
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多