add websocket
This commit is contained in:
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@@ -16,6 +16,7 @@
|
||||
<option value="$PROJECT_DIR$/foudations" />
|
||||
<option value="$PROJECT_DIR$/foudations/mogo-base-services-apk" />
|
||||
<option value="$PROJECT_DIR$/foudations/mogo-base-services-sdk" />
|
||||
<option value="$PROJECT_DIR$/foudations/mogo-base-websocket-sdk" />
|
||||
<option value="$PROJECT_DIR$/foudations/mogo-commons" />
|
||||
<option value="$PROJECT_DIR$/foudations/mogo-utils" />
|
||||
<option value="$PROJECT_DIR$/libraries" />
|
||||
|
||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -4,7 +4,7 @@
|
||||
<asm skipDebug="false" skipFrames="false" skipCode="false" expandFrames="false" />
|
||||
<groovy codeStyle="LEGACY" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="JDK" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="SuppressionsComponent">
|
||||
|
||||
@@ -356,6 +356,7 @@ dependencies {
|
||||
if (Boolean.valueOf(RELEASE)) {
|
||||
implementation rootProject.ext.dependencies.mogocommons
|
||||
implementation rootProject.ext.dependencies.modulecommon
|
||||
implementation rootProject.ext.dependencies.mogobasewebsocketsdk
|
||||
implementation rootProject.ext.dependencies.modulesearch
|
||||
implementation rootProject.ext.dependencies.mogomoduleguide
|
||||
implementation rootProject.ext.dependencies.mogomoduleauth
|
||||
@@ -373,6 +374,7 @@ dependencies {
|
||||
implementation rootProject.ext.dependencies.guideshow
|
||||
} else {
|
||||
implementation project(':foudations:mogo-commons')
|
||||
implementation project(':foudations:mogo-base-websocket-sdk')
|
||||
implementation project(':modules:mogo-module-common')
|
||||
implementation project(':modules:mogo-module-search')
|
||||
implementation project(':modules:mogo-module-authorize')
|
||||
|
||||
@@ -189,6 +189,7 @@ public class MogoApplication extends AbsMogoApplication {
|
||||
|
||||
private void prepareSocketAndLocationServices( IMogoServiceApis apis ) {
|
||||
apis.getSocketManagerApi( getApplicationContext() ).init( getApplicationContext(), DebugConfig.getSocketAppId() );
|
||||
apis.getWebSocketManagerApi( getApplicationContext() ).init( getApplicationContext(), DebugConfig.getSocketAppId() );
|
||||
apis.getLocationInfoApi().start();
|
||||
apis.getMapServiceApi().getSingletonLocationClient( getApplicationContext() ).addLocationListener( location -> {
|
||||
apis.getLocationInfoApi().provideLocation( location );
|
||||
|
||||
@@ -174,6 +174,7 @@ targetSdkVersion : 22,
|
||||
// 基础服务实现
|
||||
mogobaseservicesdk : "com.mogo.base:services-sdk:${MOGO_BASE_SERVICES_SDK_VERSION}",
|
||||
mogobaseserviceapk : "com.mogo.base:services-apk:${MOGO_BASE_SERVICES_APK_VERSION}",
|
||||
mogobasewebsocketsdk : "com.mogo.base:websocket-sdk:${MOGO_BASE_WEBSOCKET_SDK_VERSION}",
|
||||
// loglib
|
||||
mogologlib : "com.mogo.module:module-loglib:${LOGLIB_VERSION}",
|
||||
// monitor
|
||||
|
||||
1
foudations/mogo-base-websocket-sdk/.gitignore
vendored
Normal file
1
foudations/mogo-base-websocket-sdk/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
47
foudations/mogo-base-websocket-sdk/build.gradle
Normal file
47
foudations/mogo-base-websocket-sdk/build.gradle
Normal file
@@ -0,0 +1,47 @@
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
compileSdkVersion rootProject.ext.android.compileSdkVersion
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion rootProject.ext.android.minSdkVersion
|
||||
targetSdkVersion rootProject.ext.android.targetSdkVersion
|
||||
versionCode Integer.valueOf(VERSION_CODE)
|
||||
versionName getValueFromRootProperties("${project.name.replace("-", "_").toUpperCase()}_VERSION")
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: "libs", include: ["*.jar"])
|
||||
implementation rootProject.ext.dependencies.arouter
|
||||
|
||||
implementation "com.mogo.module.carchatting:module-carchatt-socket:1.1.1"
|
||||
|
||||
if (Boolean.valueOf(RELEASE)) {
|
||||
implementation rootProject.ext.dependencies.mogoutils
|
||||
implementation rootProject.ext.dependencies.mogocommons
|
||||
implementation rootProject.ext.dependencies.mogoserviceapi
|
||||
} else {
|
||||
implementation project(":foudations:mogo-utils")
|
||||
implementation project(":foudations:mogo-commons")
|
||||
implementation project(":services:mogo-service-api")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
apply from: new File(rootProject.rootDir, "gradle/upload.gradle").toString()
|
||||
3
foudations/mogo-base-websocket-sdk/gradle.properties
Normal file
3
foudations/mogo-base-websocket-sdk/gradle.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
GROUP=com.mogo.base
|
||||
POM_ARTIFACT_ID=websocket-sdk
|
||||
VERSION_CODE=1
|
||||
21
foudations/mogo-base-websocket-sdk/proguard-rules.pro
vendored
Normal file
21
foudations/mogo-base-websocket-sdk/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.mogo.mogo_base_websocket_sdk;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||
assertEquals("com.mogo.mogo_base_websocket_sdk.test", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.mogo.base.websocket">
|
||||
|
||||
/
|
||||
</manifest>
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.mogo.base.websocket;
|
||||
|
||||
public interface IMogoWebSocketInitCallBack {
|
||||
|
||||
void init();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.mogo.base.websocket;
|
||||
|
||||
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
|
||||
public class WebSocketConstant {
|
||||
|
||||
private static final String HOST_DEV = "";
|
||||
private static final String HOST_QA = "";
|
||||
private static final String HOST_DEMO = "";
|
||||
private static final String HOST_RELEASE = "";
|
||||
|
||||
public static String getSocketServer(){
|
||||
switch (DebugConfig.getNetMode()){
|
||||
case DebugConfig.NET_MODE_DEV:
|
||||
return HOST_DEV;
|
||||
case DebugConfig.NET_MODE_QA:
|
||||
return HOST_QA;
|
||||
case DebugConfig.NET_MODE_DEMO:
|
||||
return HOST_DEMO;
|
||||
case DebugConfig.NET_MODE_RELEASE:
|
||||
default:
|
||||
return HOST_RELEASE;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.mogo.base.websocket;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class WebSocketData<T> implements Serializable {
|
||||
|
||||
private long seq;
|
||||
|
||||
private int msgType;
|
||||
|
||||
private T t;
|
||||
|
||||
public long getSeq() {
|
||||
return seq;
|
||||
}
|
||||
|
||||
public void setSeq(long seq) {
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
public int getMsgType() {
|
||||
return msgType;
|
||||
}
|
||||
|
||||
public void setMsgType(int msgType) {
|
||||
this.msgType = msgType;
|
||||
}
|
||||
|
||||
public T getT() {
|
||||
return t;
|
||||
}
|
||||
|
||||
public void setT(T t) {
|
||||
this.t = t;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.mogo.base.websocket;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Message;
|
||||
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import com.mogo.websocket.SocketClient;
|
||||
|
||||
public class WebSocketHandlerThread extends HandlerThread {
|
||||
|
||||
private Handler mHandler;
|
||||
private String tag;
|
||||
|
||||
private static final int MSG_SEND = 1;
|
||||
|
||||
public WebSocketHandlerThread(String name) {
|
||||
super(name);
|
||||
tag = name;
|
||||
mHandler = new Handler(this.getLooper()) {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (msg.what == MSG_SEND) {
|
||||
Logger.d(tag, "WebSocketHandlerThread handleMessage = %d", msg.obj.toString());
|
||||
SocketClient.getInstance().getClientProxy().sendMessage(msg.obj.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void sendMsg(String msg) {
|
||||
Message message = new Message();
|
||||
message.what = MSG_SEND;
|
||||
message.obj = msg;
|
||||
mHandler.sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.mogo.base.websocket;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.Keep;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.mogo.commons.network.Utils;
|
||||
import com.mogo.service.connection.IMogoMsgAckListener;
|
||||
import com.mogo.service.connection.IMogoOnWebSocketMessageListener;
|
||||
import com.mogo.service.connection.IMogoWebSocketManager;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import com.mogo.utils.network.utils.GsonUtil;
|
||||
import com.mogo.websocket.ISocketMsgCallBack;
|
||||
import com.mogo.websocket.ISocketMsgSetting;
|
||||
import com.mogo.websocket.SocketClient;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static com.mogo.service.connection.WebSocketMsgType.MSG_TYPE_SERVER;
|
||||
|
||||
@Keep
|
||||
public class WebSocketManager implements IMogoWebSocketManager, ISocketMsgSetting, ISocketMsgCallBack {
|
||||
|
||||
private static final String TAG = "WebSocketManager-sdk";
|
||||
|
||||
private static volatile WebSocketManager sInstance;
|
||||
private WebSocketHandlerThread handlerThread;
|
||||
|
||||
public WebSocketManager(Context context) {
|
||||
handlerThread = new WebSocketHandlerThread(TAG);
|
||||
handlerThread.start();
|
||||
}
|
||||
|
||||
@Keep
|
||||
public static WebSocketManager getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
synchronized (WebSocketManager.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new WebSocketManager(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理消息分发
|
||||
* <p>
|
||||
* key - msgType
|
||||
*/
|
||||
private Map<Integer, List<IMogoOnWebSocketMessageListener>> mListeners = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 管理消息回执
|
||||
* <p>
|
||||
* key - msgId
|
||||
*/
|
||||
private Map<Long, IMogoMsgAckListener> mAckListeners = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void init(Context context, String appId) {
|
||||
SocketClient.getInstance().getClientProxy().initSocketServer(WebSocketConstant.getSocketServer());
|
||||
SocketClient.getInstance().getClientProxy().getMessageSettings(this);
|
||||
SocketClient.getInstance().getClientProxy().addISocketMsgCallBack(this);
|
||||
SocketClient.getInstance().getClientProxy().startConnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerOnWebSocketMessageListener(IMogoOnWebSocketMessageListener listener) {
|
||||
if (listener.getType() == null) {
|
||||
Logger.w(TAG, "websocket registerListener msgType is not exist");
|
||||
return;
|
||||
}
|
||||
int msgType = listener.getType().getMsgType();
|
||||
if (mListeners.containsKey(msgType)) {
|
||||
Logger.w(TAG, "websocket msgType %d is exist.", msgType);
|
||||
}
|
||||
if (!mListeners.containsKey(msgType)) {
|
||||
mListeners.put(msgType, new ArrayList<>());
|
||||
}
|
||||
mListeners.get(msgType).add(listener);
|
||||
Logger.d(TAG, "websocket add listener success");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterOnWebSocketMessageListener(IMogoOnWebSocketMessageListener listener) {
|
||||
if (listener == null) {
|
||||
return;
|
||||
}
|
||||
if (listener.getType() == null) {
|
||||
Logger.w(TAG, "websocket unregisterListener msgType is not exist");
|
||||
return;
|
||||
}
|
||||
int msgType = listener.getType().getMsgType();
|
||||
if (!mListeners.containsKey(msgType)) {
|
||||
return;
|
||||
}
|
||||
List<IMogoOnWebSocketMessageListener> listeners = mListeners.get(msgType);
|
||||
if (listeners != null && listeners.contains(listener)) {
|
||||
listeners.remove(listener);
|
||||
Logger.d(TAG, "websocket remove listener success");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMsg(Object body, IMogoOnWebSocketMessageListener listener) {
|
||||
Logger.d(TAG, "websocket sendMsg body = %d , listener = %d ", body, listener);
|
||||
if (handlerThread != null) {
|
||||
WebSocketData webSocketData = new WebSocketData();
|
||||
webSocketData.setSeq(System.currentTimeMillis());
|
||||
webSocketData.setMsgType(listener.getType().getMsgType());
|
||||
webSocketData.setT(body);
|
||||
handlerThread.sendMsg(new Gson().toJson(webSocketData));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Context context) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectOpen() {
|
||||
Logger.d(TAG, "websocket onConnectOpen");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(String message) {
|
||||
Logger.d(TAG, "websocket received msg = %d ", message);
|
||||
WebSocketData webSocketData = GsonUtil.objectFromJson(message, WebSocketData.class);
|
||||
int msgType = webSocketData.getMsgType();
|
||||
Logger.d(TAG, "websocket received msg type = %d", msgType);
|
||||
//服务端下发数据返回,上传数据回执默认不返回
|
||||
if (msgType == MSG_TYPE_SERVER.getMsgType()) {
|
||||
List<IMogoOnWebSocketMessageListener> listeners = mListeners.get(msgType);
|
||||
if (listeners != null && !listeners.isEmpty()) {
|
||||
Iterator<IMogoOnWebSocketMessageListener> iterator = listeners.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
IMogoOnWebSocketMessageListener listener = iterator.next();
|
||||
if (listener != null) {
|
||||
Logger.d(TAG, "received msgId = %s, content = %s", webSocketData.getSeq(), webSocketData.getT().toString());
|
||||
listener.onMsgReceived(webSocketData.getT());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectClose() {
|
||||
Logger.d(TAG, "websocket ready to reconnect");
|
||||
SocketClient.getInstance().getClientProxy().reConnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(Exception e) {
|
||||
Logger.e(TAG, "websocket handleError : %d", e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHandShakeMsg() {
|
||||
WebSocketShakeHandsData webSocketShakeHandData = new WebSocketShakeHandsData(
|
||||
System.currentTimeMillis(),
|
||||
Utils.getSn(),
|
||||
true
|
||||
);
|
||||
return GsonUtil.jsonFromObject(webSocketShakeHandData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeartBeatMsg() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.mogo.base.websocket;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class WebSocketShakeHandsData implements Serializable {
|
||||
|
||||
public WebSocketShakeHandsData(){
|
||||
|
||||
}
|
||||
|
||||
public WebSocketShakeHandsData(long seq, String sn, boolean bind) {
|
||||
this.seq = seq;
|
||||
this.sn = sn;
|
||||
this.bind = bind;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求序列号
|
||||
*/
|
||||
private long seq;
|
||||
|
||||
/**
|
||||
* 车机sn
|
||||
*/
|
||||
private String sn;
|
||||
|
||||
/**
|
||||
* 是否申请通道绑定
|
||||
*/
|
||||
private boolean bind;
|
||||
|
||||
public long getSeq() {
|
||||
return seq;
|
||||
}
|
||||
|
||||
public void setSeq(long seq) {
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
public String getSn() {
|
||||
return sn;
|
||||
}
|
||||
|
||||
public void setSn(String sn) {
|
||||
this.sn = sn;
|
||||
}
|
||||
|
||||
public boolean isBind() {
|
||||
return bind;
|
||||
}
|
||||
|
||||
public void setBind(boolean bind) {
|
||||
this.bind = bind;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.mogo.mogo_base_websocket_sdk;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,7 @@ MOGO_MODULE_V2X_VERSION=2.0.0
|
||||
# 基础服务实现:passport、socket、location
|
||||
MOGO_BASE_SERVICES_APK_VERSION = 2.0.0
|
||||
MOGO_BASE_SERVICES_SDK_VERSION = 2.0.0
|
||||
MOGO_BASE_WEBSOCKET_SDK_VERSION = 1.0.0
|
||||
## 工程外部模块
|
||||
# 探路
|
||||
MOGO_MODULE_TANLU_VERSION=2.0.0
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.mogo.service.analytics.IMogoAnalytics;
|
||||
import com.mogo.service.auth.IMogoAuthManager;
|
||||
import com.mogo.service.cardmanager.IMogoCardManager;
|
||||
import com.mogo.service.connection.IMogoSocketManager;
|
||||
import com.mogo.service.connection.IMogoWebSocketManager;
|
||||
import com.mogo.service.datamanager.IMogoDataManager;
|
||||
import com.mogo.service.entrance.IMogoEntranceButtonController;
|
||||
import com.mogo.service.eventpanel.IEventPanelProvider;
|
||||
@@ -69,6 +70,11 @@ public interface IMogoServiceApis extends IProvider {
|
||||
*/
|
||||
IMogoSocketManager getSocketManagerApi( Context context );
|
||||
|
||||
/**
|
||||
* 获取WebSocket操作api
|
||||
*/
|
||||
IMogoWebSocketManager getWebSocketManagerApi( Context context );
|
||||
|
||||
/**
|
||||
* 大而全的数据管理接口
|
||||
*
|
||||
|
||||
@@ -51,6 +51,12 @@ public class MogoServicePaths {
|
||||
@Deprecated
|
||||
public static final String PATH_SOCKET_MANAGER = "/socket/manager";
|
||||
|
||||
/**
|
||||
* WebSocket 长链
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String PATH_WEB_SOCKET_MANAGER = "/websocket/manager";
|
||||
|
||||
/**
|
||||
* 状态管理接口
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.mogo.service.connection;
|
||||
|
||||
/**
|
||||
* 消息回调
|
||||
*/
|
||||
public interface IMogoOnWebSocketMessageListener< T > {
|
||||
|
||||
WebSocketMsgType getType();
|
||||
|
||||
void onMsgReceived(T obj);
|
||||
|
||||
void onError(String errorMsg);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.mogo.service.connection;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.alibaba.android.arouter.facade.template.IProvider;
|
||||
|
||||
public interface IMogoWebSocketManager<T> extends IProvider {
|
||||
|
||||
/**
|
||||
* 初始化,各模块不用关心
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param appId 一般为包名,不参与通道的建立,一般用于发消息
|
||||
*/
|
||||
void init( Context context, String appId );
|
||||
|
||||
/**
|
||||
* 注册消息监听
|
||||
*
|
||||
* @param listener 回调
|
||||
*/
|
||||
void registerOnWebSocketMessageListener(IMogoOnWebSocketMessageListener listener );
|
||||
|
||||
/**
|
||||
* 注销消息监听
|
||||
*
|
||||
*/
|
||||
void unregisterOnWebSocketMessageListener(IMogoOnWebSocketMessageListener listener );
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param body 消息体
|
||||
* @param listener 回执监听
|
||||
*/
|
||||
void sendMsg( T body, IMogoOnWebSocketMessageListener listener );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.mogo.service.connection;
|
||||
|
||||
public enum WebSocketMsgType {
|
||||
|
||||
MSG_TYPE_SELF(0,"自车数据"),
|
||||
MSG_TYPE_ADAS(1,"ADAS数据"),
|
||||
MSG_TYPE_SERVER(2,"服务端下发车辆信息");
|
||||
|
||||
private int msgType;
|
||||
private String msg;
|
||||
|
||||
WebSocketMsgType(int msgType, String msg) {
|
||||
this.msgType = msgType;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public int getMsgType() {
|
||||
return msgType;
|
||||
}
|
||||
|
||||
public void setMsgType(int msgType) {
|
||||
this.msgType = msgType;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import com.mogo.service.analytics.IMogoAnalytics;
|
||||
import com.mogo.service.auth.IMogoAuthManager;
|
||||
import com.mogo.service.cardmanager.IMogoCardManager;
|
||||
import com.mogo.service.connection.IMogoSocketManager;
|
||||
import com.mogo.service.connection.IMogoWebSocketManager;
|
||||
import com.mogo.service.datamanager.IMogoDataManager;
|
||||
import com.mogo.service.entrance.IMogoEntranceButtonController;
|
||||
import com.mogo.service.eventpanel.IEventPanelProvider;
|
||||
@@ -72,6 +73,11 @@ public class MogoServiceApis implements IMogoServiceApis {
|
||||
return getApiInstance( IMogoSocketManager.class, MogoServicePaths.PATH_SOCKET_MANAGER );
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMogoWebSocketManager getWebSocketManagerApi(Context context) {
|
||||
return getApiInstance( IMogoWebSocketManager.class, MogoServicePaths.PATH_WEB_SOCKET_MANAGER );
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMogoFragmentManager getFragmentManagerApi() {
|
||||
return SingletonsHolder.get( IMogoFragmentManager.class );
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.mogo.service.impl.connection;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.alibaba.android.arouter.facade.annotation.Route;
|
||||
import com.mogo.service.connection.IMogoOnWebSocketMessageListener;
|
||||
import com.mogo.service.connection.IMogoWebSocketManager;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static com.mogo.service.MogoServicePaths.PATH_WEB_SOCKET_MANAGER;
|
||||
|
||||
@Route(path = PATH_WEB_SOCKET_MANAGER)
|
||||
public class MogoWebSocketManager implements IMogoWebSocketManager {
|
||||
|
||||
private static final String TAG = "MogoWebSocketManager";
|
||||
private IMogoWebSocketManager mDelegate;
|
||||
|
||||
@Override
|
||||
public void init(Context context, String appId) {
|
||||
if (mDelegate != null) {
|
||||
mDelegate.init(context, appId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerOnWebSocketMessageListener(int msgType, IMogoOnWebSocketMessageListener listener) {
|
||||
if (mDelegate != null) {
|
||||
mDelegate.registerOnWebSocketMessageListener(msgType, listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterOnWebSocketMessageListener(int msgType, IMogoOnWebSocketMessageListener listener) {
|
||||
if (mDelegate != null) {
|
||||
mDelegate.unregisterOnWebSocketMessageListener(msgType, listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMsg(Object body, IMogoOnWebSocketMessageListener listener) {
|
||||
if (mDelegate != null) {
|
||||
mDelegate.sendMsg(body, listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Context context) {
|
||||
try {
|
||||
Class<?> clazz = Class.forName("com.mogo.base.websocket.WebSocketManager");
|
||||
Method getInstanceMethod = clazz.getMethod("getInstance", Context.class);
|
||||
getInstanceMethod.setAccessible(true);
|
||||
mDelegate = (IMogoWebSocketManager) getInstanceMethod.invoke(null, context);
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, e, "error.");
|
||||
}
|
||||
|
||||
if (mDelegate != null) {
|
||||
mDelegate.init(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
include ':foudations:mogo-base-websocket-sdk'
|
||||
include ':tts:tts-base'
|
||||
include ':tts:tts-di'
|
||||
include ':tts:tts-zhi'
|
||||
|
||||
Reference in New Issue
Block a user