开发环境:
Android Studio v3.5
JDK 1.8
代码实现:
一、连接Websocket,与呼叫管理平台建立长连接:
//连接ws
String wsUrl = mEtWsAddr.getText().toString().trim();
String authNo = mEtAuthNo.getText().toString().trim();
WebSocketManager.getInstance().connect(wsUrl, authNo);
1.1:ws连接成功后,发送登录指令:
/**
* 发送登录指令
*/
public void sendLoginCmd() {
sendMsg(GsonHelper.getInstance().toJsonStr(new WSMessage("login", mDeviceNo)));
}
1.2:登录成功后,定时发送心跳:
/**
* 发送心跳,10s一次
*/
public void sendHeartBeat() {
if (mWebSocketClient != null) {
long abs = Math.abs(SystemClock.elapsedRealtime() - lastHeartBeatTime);
if (abs > HEART_BEAT_TIME_OUT) {
L.i("心跳超时");
LogThread.getInstance().write("WebSocket", "心跳超时,开始重连");
//心跳超时,重连服务器
reConnect();
}
sendMsg("ping");
Message msg = Message.obtain();
msg.what = TYPE_HEART_BEAT;
mHandler.sendMessageDelayed(msg, HEART_BEAT_INTERVAL);
}
}
二、处理Websocket下发的消息:
/**
* 下发消息处理
*
* @param jObj
* @return
*/
private String dealReceivedMsg(JSONObject jObj) {
try {
String actionType = jObj.optString("actionType");
if ("pullData".equals(actionType)) {
if (jObj.has("sessionPullMsg")) {
String sessionPullMsg = jObj.optString("sessionPullMsg");
PullMsg pullMsg = GsonHelper.getInstance().fromJson(sessionPullMsg, PullMsg.class);
if (pullMsg != null && !TextUtils.isEmpty(pullMsg.getAction())) {
L.i("pullMsg:" + pullMsg.toString());
LogThread.getInstance().write(TAG, sessionPullMsg);
switch (pullMsg.getAction()) {
case "applycall":
return mWsProtocolParser.startIntercom(jObj.getString("sessionId"), jObj.getString("terminalId"), pullMsg);
case "closecall":
return mWsProtocolParser.stopIntercom(jObj.getString("sessionId"), jObj.getString("terminalId"), pullMsg);
default: {
return GsonHelper.getInstance().toJsonStr(new PullMsgResp(WsProtocolParser.CODE_FAILURE + "", String.format("下发的action[%s] 无效", pullMsg.getAction()), pullMsg.getAction(), ""));
}
}
} else {
JSONObject subPullMsg = new JSONObject(sessionPullMsg);
String actionName = subPullMsg.getString("actionName");
if (!TextUtils.isEmpty(actionName)) {
return mWsProtocolParser.syncServerTime(jObj.getString("sessionId"), jObj.getString("terminalId"), pullMsg, actionName);
}
return GsonHelper.getInstance().toJsonStr(new PullMsgResp(WsProtocolParser.CODE_FAILURE + "", "sessionPullMsg解析失败", "", ""));
}
} else {
return GsonHelper.getInstance().toJsonStr(new PullMsgResp(WsProtocolParser.CODE_FAILURE + "", "sessionPullMsg不存在", "", ""));
}
} else {
return GsonHelper.getInstance().toJsonStr(new PullMsgResp(WsProtocolParser.CODE_FAILURE + "", "actionType不为pullData", "", ""));
}
} catch (Exception e) {
return GsonHelper.getInstance().toJsonStr(new PullMsgResp(WsProtocolParser.CODE_FAILURE + "", "数据解析错误:" + e.getMessage(), "", ""));
}
}
三、接听处理:
private void startConnectServer(final String url, final String deviceID, final String userID, final String passWord) {
LiveManager.setContext(this);
LiveManager.getIntance().addEventListen(mCallback);
LiveManager.getIntance().startInitLive(url, deviceID, userID, passWord);
}
......
public void initData() {
PhoneLogger.i("显示视频:" + enableVideo);
remoteVideoView.setVisibility(enableVideo ? View.VISIBLE : View.GONE);
resetCallData(intentParam.getDevicename(), intentParam.getParkingname());
LiveManager.getIntance().addEventListen(mCallback);
LiveManager.getIntance().liveConnect(remoteVideoView, enableVideo);
}
四、挂断处理:
LiveManager.getIntance().stopLive();
- demo下载
接听终端demo(Android).zip
下载地址:http://doc.cpyht.com/server/index.php?s=/api/attachment/visitFile&sign=2e8d557937e24c1d46edc5f3ac716a34
作者:郭祥西 创建时间:2024-04-10 17:40
最后编辑:郭祥西 更新时间:2024-11-07 15:18
最后编辑:郭祥西 更新时间:2024-11-07 15:18