# AndroidWebSocket **Repository Path**: yedong/android-websocket ## Basic Information - **Project Name**: AndroidWebSocket - **Description**: Android WebSocket - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-07-21 - **Last Updated**: 2025-03-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #### 项目根目录的 build.gradle 添加repository ``` allprojects { repositories { ... maven { url 'https://jitpack.io' } } } ``` #### 添加依赖:在项目module下gradle加入: ```gradle implementation 'com.gitee.yedong:android-websocket:1.0.0' ``` #### 初始化 ```kotlin //先初始化 WebSocketKit.init(this, BuildConfig.debug) ``` #### 设置参数,添加监听 ```kotlin WebSocketKit.defaultClient() .setConfig(yourConfig) //自定义 OkHttp .setOkHttp( OkHttpClient.Builder() .pingInterval(3, TimeUnit.SECONDS) // 设置心跳间隔,这个是3秒检测一次 .build()) .addListener(object : SocketCallback { override fun onOpen() { Log.d("MainActivity", "**********onOpen:") } override fun onMessage(message: String) { Log.d("MainActivity", "**********返回数据:$message") } override fun onMessage(bytes: ByteString) { Log.d("MainActivity", "**********返回数据:$bytes") } override fun onReconnect() { Log.d("MainActivity", "**********重连:") } override fun onClose(reason: String) { Log.d("MainActivity", "**********关闭") } }) ``` #### 建立连接 ```kotlin WebSocketKit.defaultClient().setUrl("your url").startConnect() ``` #### 发送消息 ### ```kotlin WebSocketKit.defaultClient().send("hello word"); WebSocketKit.defaultClient().send(url, ByteString.EMPTY) ``` Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.