Skip to content

websocket

StormKid edited this page Sep 4, 2019 · 3 revisions

1、初始化:

     OkWebsocket.instance.Config().setTimeOut(15000L).showLog(true)

说明:此两项为了开发的合理性,必须初始化两个方法,不然主动会抛异常。

2、初始化并处理回调:

     private val url = "ws://xxxxx"
     OkWebsocket.instance.Builder().setUrl(url).build().startSocket(object :
                WebsocketCallbackRule<String> {
                override suspend fun onMessageSuccess(massage: String) {
                    click.text = massage
                }

                override suspend fun onSocketDrop(err: String) {
                }

                override suspend fun onClosed() {
                }

            })

3、发送给websocket 服务端信息:

    // 这里已经自行处理成json对象传递给后端,此处可以发任意信息
    OkWebsocket.instance.sendMsg(any)
    // ---------------------------------------------
    // 例如:
    OkWebsocket.instance.sendMsg(hashMapOf("event" to "message"))
Clone this wiki locally