【问题标题】:Unable to connect to a node.js socket.io 3.x server from Android socket.io 2 client无法从 Android socket.io 2 客户端连接到 node.js socket.io 3.x 服务器
【发布时间】:2021-04-02 14:23:32
【问题描述】:

我正在尝试将 android/kotlin 上的 socket.io 客户端与节点 js 套接字 io 服务器一起使用

主活动:

import io.socket.emitter.Emitter
import io.socket.engineio.client.Socket


override fun onCreate(savedInstanceState: Bundle?) {
    //...
    val mSocket =  Socket("http://192.168.1.15:3000")
    mSocket.on(Socket.EVENT_ERROR, { print("error")})
    mSocket.on(Socket.EVENT_OPEN, Emitter.Listener() {
        mSocket.emit("mayaCommand", "cmds.polyCube()");
        mSocket.close()
        print("ok")
     )}
    mSocket.open()
    
    

Socketio 库:

 implementation 'io.socket:engine.io-client:2.0.0'.

在这个库 (here) 的 github 页面上,2.0.0 版适用于 3.x 服务器,所以我的 nodejs socket.io 服务器在 3.0.3 上

"socket.io": "3.0.3",
"socket.io-client": "3.0.3"

日志中没有任何内容,只是不工作,我用 Wireshark 检查了网络数据包,我可以看到一些数据包。

我已经创建了一个 network_security_config.xml :

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://192.168.1.15</domain>
    </domain-config>
</network-security-config> 

我的清单中的链接:

 <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:networkSecurityConfig="@xml/network_security_config"
        android:usesCleartextTraffic="true"
       >

而且不工作,什么也没有发生。

谢谢

【问题讨论】:

    标签: android node.js sockets kotlin socket.io


    【解决方案1】:

    好的, 所以今天我重新创建了整个项目,一步一步地在我的另一个屏幕上运行wireshark。

    并且破坏:它正在工作。 我会详细说明所有步骤,以防其他人遇到同样的问题。

    将此添加到 gradle 模块文件中:

     implementation('io.socket:socket.io-client:2.0.0') {
            exclude group: 'org.json', module: 'json'
        }
    

    (2.0.0 用于 socket.io 3.x 服务器)

    编辑清单:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.socketdccs">
        <uses-permission android:name="android.permission.INTERNET"  /> <=== add perms
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            android:usesCleartextTraffic="true" <==== this is important
    /...  
    

    还有我的 MainActivity :

    package com.example.myapp
    // ...
    import io.socket.client.IO
    import io.socket.client.Socket
    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
    
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            
    
            val mSocket = IO.socket("http://192.168.1.15:3000")
            mSocket.on(Socket.EVENT_CONNECT, {println("connected")})
            testBtn.setOnClickListener {
    
                mSocket.connect()
                mSocket.emit("mayaCommand", "cmds.polyCube()")
                println("ok")
            }
    
        }
    }
    

    现在,它工作正常。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 2015-06-29
      • 2017-04-22
      相关资源
      最近更新 更多