Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多连接时接收数据经常错乱 #69

Open
panyingdaoz opened this issue Feb 21, 2024 · 0 comments
Open

多连接时接收数据经常错乱 #69

panyingdaoz opened this issue Feb 21, 2024 · 0 comments

Comments

@panyingdaoz
Copy link

当接收到数据后,连接1回调经常接收到其它连接返回的数据,回调的数据完全乱了;数据的接收是10-50毫秒级的
我连接配置代码如下
for (i in 0..7) {
val socketSettings = WebSocketSetting()
val url = getSocketUrl(i)
LogUtil.e("webSocket初始化 $i 地址:$url")
//连接地址,必填,例如 wss://echo.websocket.org
socketSettings.connectUrl = url //必填
//设置连接超时时间
socketSettings.connectTimeout = 5 * 1000
//设置心跳间隔时间
socketSettings.connectionLostTimeout = 60
//设置断开后的重连次数,可以设置的很大,不会有什么性能上的影响
socketSettings.reconnectFrequency = 60

        //设置消息分发器,接收到数据后先进入该类中处理,处理完再发送到下游
        socketSettings.setResponseProcessDispatcher(AppResponseDispatcher())
        //接收到数据后是否放入子线程处理,只有设置了 ResponseProcessDispatcher 才有意义
        socketSettings.setProcessDataOnBackground(true)
        //需要调用 WebSocketHandler.registerNetworkChangedReceiver(context) 方法注册网络监听广播
        socketSettings.setReconnectWithNetworkChanged(true)
        val manager = getWebSocketManager(i, url, socketSettings)
        //启动连接
        manager.start()
    }

    //注意,需要在 AndroidManifest 中配置网络状态获取权限
    //注册网路连接状态变化广播
    WebSocketHandler.registerNetworkChangedReceiver(this)

下面是监听添加代码
private fun addListener() {
// socket监听
WebSocketHandler.getWebSocket(MyApplication.getWst(0)).addListener(socketListener0)
WebSocketHandler.getWebSocket(MyApplication.getWst(1)).addListener(socketListener1)
WebSocketHandler.getWebSocket(MyApplication.getWst(2)).addListener(socketListener2)
WebSocketHandler.getWebSocket(MyApplication.getWst(3)).addListener(socketListener3)
WebSocketHandler.getWebSocket(MyApplication.getWst(4)).addListener(socketListener4)
ThreadPoolManager.getInstance().execute {
WebSocketHandler.getWebSocket(MyApplication.getWst(5)).addListener(socketListener5)
WebSocketHandler.getWebSocket(MyApplication.getWst(6)).addListener(socketListener6)
WebSocketHandler.getWebSocket(MyApplication.getWst(99)).addListener(socketListener99)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant