1、GitHub地址
https://github.com/TooTallNate/Java-WebSocket
implementation "org.java-websocket:Java-WebSocket:1.5.1"
2、项目新建module->im_websocket

module->build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode 1
versionName "1.0"
}
}
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.java-websocket:Java-WebSocket:1.5.1"
}
其中配置文件来自统一配置文件config.gradle
ext {
android = [
compileSdkVersion: 28,
buildToolsVersion: "28.0.3",
minSdkVersion : 21,
targetSdkVersion : 28,
versionCode : VERSION_CODE as int,
versionName : APP_VERSION
]
version = [
androidSupportSdkVersion: "28.0.0",
retrofitSdkVersion : "2.2.0",
butterknifeSdkVersion : "8.8.1",
constraintLayout : "1.1.2",
espressoSdkVersion : "2.2.2",
canarySdkVersion : "1.5.1",
dagger2SdkVersion : "2.10",
rxlifecycleSdkVersion : "1.0",
rxlifecycle2SdkVersion : "2.0.1"
]
dependencies = [
//support
"appcompat-v7": "com.android.support:appcompat-v7:${version["androidSupportSdkVersion"]}",
...
]
...
}
3、封装
JWebSocketClient.java文件:
package com.example.im_websocket;
import android.util.Log;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
public class JWebSocketClient extends WebSocketClient {
public JWebSocketClient(URI serverUri) {
super(serverUri, new Draft_6455());
}
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.e("JWebSocketClient", "onOpen()");
}
@Override
public void onMessage(String message) {
Log.e("JWebSocketClient", "onMessage()");
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.e("JWebSocketClient", "onClose()");
}
@Override
public void onError(Exception ex) {
Log.e("JWebSocketClient", "onError()");
}
}
ImClient:
package com.example.im_websocket;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
public class ImClient {
public JWebSocketClient client;
private static String TAG = "ImClient";
private URI allUri = null;
private static ImClient.OnReceiveMessageListener onReceiveMessageListener;
public static void init(Context context, String appKey) {
init(context, appKey, true);
}
private static void init(Context context, String appKey, boolean isPush) {
if (TextUtils.isEmpty(appKey)) {
Log.e(TAG, "appkey is null");
return;
}
SingletonHolder.sInstance.initClientSet(context, appKey);
}
//连接服务器
public static ImClient connect(final ConnectCallback callback) {
SingletonHolder.sInstance.connectServer(new ConnectCallback() {
@Override
public void onSuccess(String var1) {
if (callback != null)
callback.onSuccess(var1);
}
@Override
public void onError(String var1) {
if (callback != null)
callback.onError(var1);
}
@Override
public void onMessage(String message) {
if (callback != null)
callback.onMessage(message);
}
@Override
public void onTokenIncorrect() {
if (callback != null)
callback.onTokenIncorrect();
}
});
return SingletonHolder.sInstance;
}
public static void disConnectionSocket() {
SingletonHolder.sInstance.closeConnect();
}
public static void reConnectionWebsoket() throws Exception{
SingletonHolder.sInstance.reConnectionSelfWebsoket();
}
public static boolean isNullReconnectionWebsocket() {
return SingletonHolder.sInstance.isNullReconnectionSelfWebsocket();
}
public static boolean isCloseConnectionWebsocket() {
return SingletonHolder.sInstance.isCloseReconnectionSelfWebsocket();
}
public static void setOnReceiveMessageListener(OnReceiveMessageListener listener) {
onReceiveMessageListener = listener;
}
private void initClientSet(Context context, String appKey) {
allUri = URI.create(appKey);
}
private void connectServer(final ConnectCallback connectCallback) {
try {
if (client == null) {
client = new JWebSocketClient(allUri) {
@Override
public void onMessage(String message) {
Log.e(TAG, "onMessage message=" + message);
if (connectCallback != null)
connectCallback.onMessage(message);
if (onReceiveMessageListener != null) {
onReceiveMessageListener.onReceived(message, 1);
}
}
@Override
public void onOpen(ServerHandshake handshakedata) {
super.onOpen(handshakedata);
Log.e(TAG, "onOpen =" + handshakedata.getHttpStatusMessage());
if (connectCallback != null)
connectCallback.onSuccess(handshakedata.getHttpStatusMessage());
}
@Override
public void onClose(int code, String reason, boolean remote) {
super.onClose(code, reason, remote);
Log.e(TAG, "onClose code=" + code + ",reason=" + reason + ",remote" + remote);

本文详细介绍如何在Android项目中使用Java-WebSocket库实现WebSocket功能,包括模块搭建、配置、封装及调用流程,适用于即时通讯场景。
最低0.47元/天 解锁文章
537

被折叠的 条评论
为什么被折叠?



