mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-27 02:20:32 +08:00
对websdk和javasdk增加了处理下线的逻辑
This commit is contained in:
parent
13805095b5
commit
627e2bcd9a
@ -26,8 +26,8 @@ import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
class CIMCacheManager {
|
||||
|
||||
class CIMCacheManager {
|
||||
|
||||
public static final String CIM_CONFIG_INFO = "CIM_CONFIG_INFO";
|
||||
|
||||
@ -45,16 +45,12 @@ class CIMCacheManager {
|
||||
|
||||
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
|
||||
|
||||
|
||||
public static void remove(Context context ,String key)
|
||||
{
|
||||
public static void remove(Context context, String key) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
resolver.delete(Uri.parse(CIMCacheProvider.CONTENT_URI), key, null);
|
||||
}
|
||||
|
||||
|
||||
public static void putString(Context context ,String key,String value)
|
||||
{
|
||||
public static void putString(Context context, String key, String value) {
|
||||
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
ContentValues values = new ContentValues();
|
||||
@ -64,13 +60,11 @@ class CIMCacheManager {
|
||||
|
||||
}
|
||||
|
||||
public static String getString(Context context ,String key)
|
||||
{
|
||||
public static String getString(Context context, String key) {
|
||||
String value = null;
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
Cursor cursor = resolver.query(Uri.parse(CIMCacheProvider.CONTENT_URI), new String[] { key }, null, null, null);
|
||||
if (cursor!=null && cursor.moveToFirst())
|
||||
{
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
value = cursor.getString(0);
|
||||
cursor.close();
|
||||
}
|
||||
@ -82,28 +76,24 @@ class CIMCacheManager {
|
||||
try {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}catch(Exception e){}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void putBoolean(Context context,String key,boolean value)
|
||||
{
|
||||
public static void putBoolean(Context context, String key, boolean value) {
|
||||
putString(context, key, Boolean.toString(value));
|
||||
}
|
||||
|
||||
public static boolean getBoolean(Context context,String key)
|
||||
{
|
||||
public static boolean getBoolean(Context context, String key) {
|
||||
String value = getString(context, key);
|
||||
return value == null ? false : Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
|
||||
public static void putInt(Context context,String key,int value)
|
||||
{
|
||||
public static void putInt(Context context, String key, int value) {
|
||||
putString(context, key, String.valueOf(value));
|
||||
}
|
||||
|
||||
public static int getInt(Context context,String key)
|
||||
{
|
||||
public static int getInt(Context context, String key) {
|
||||
String value = getString(context, key);
|
||||
return value == null ? 0 : Integer.parseInt(value);
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ public class CIMCacheProvider extends ContentProvider {
|
||||
public static final String CONTENT_URI = "content://com.farsunset.cim.provider";
|
||||
static final String MODEL_KEY = "PRIVATE_CIM_CONFIG";
|
||||
|
||||
|
||||
@Override
|
||||
public int delete(Uri arg0, String key, String[] arg2) {
|
||||
getContext().getSharedPreferences(MODEL_KEY, Context.MODE_PRIVATE).edit().remove(key).apply();
|
||||
@ -70,5 +69,4 @@ public class CIMCacheProvider extends ContentProvider {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
@ -75,8 +76,6 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
|
||||
private static CIMConnectorManager manager;
|
||||
|
||||
|
||||
|
||||
private CIMConnectorManager(Context ctx) {
|
||||
context = ctx;
|
||||
|
||||
@ -150,8 +149,7 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
}
|
||||
|
||||
executor.execute(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
public void run() {
|
||||
syncConnection(host, port);
|
||||
}
|
||||
});
|
||||
@ -165,14 +163,12 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
String exceptionName = SessionClosedException.class.getSimpleName();
|
||||
|
||||
IoSession session = getCurrentSession();
|
||||
if(session!=null && session.isConnected())
|
||||
{
|
||||
if (session != null && session.isConnected()) {
|
||||
WriteFuture wf = session.write(body);
|
||||
// 消息发送超时 5秒
|
||||
wf.awaitUninterruptibly(WRITE_TIMEOUT);
|
||||
isSuccessed = wf.isWritten();
|
||||
if(wf.getException() != null)
|
||||
{
|
||||
if (wf.getException() != null) {
|
||||
exceptionName = wf.getException().getClass().getSimpleName();
|
||||
}
|
||||
}
|
||||
@ -205,22 +201,16 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
return session != null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void closeSession()
|
||||
{
|
||||
public void closeSession() {
|
||||
IoSession session = getCurrentSession();
|
||||
if(session!=null)
|
||||
{
|
||||
if (session != null) {
|
||||
session.closeNow();
|
||||
}
|
||||
}
|
||||
|
||||
public IoSession getCurrentSession()
|
||||
{
|
||||
public IoSession getCurrentSession() {
|
||||
Map<Long, IoSession> sessions = connector.getManagedSessions();
|
||||
for(Long key:sessions.keySet())
|
||||
{
|
||||
for (Long key : sessions.keySet()) {
|
||||
IoSession session = sessions.get(key);
|
||||
if (session.isConnected()) {
|
||||
return session;
|
||||
@ -229,11 +219,9 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sessionCreated(IoSession session) throws Exception {
|
||||
|
||||
|
||||
Log.i(TAG, "****************CIM连接服务器成功:" + session.getLocalAddress() + " NID:" + session.getId());
|
||||
|
||||
setLastHeartbeatTime(session);
|
||||
@ -257,16 +245,15 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
|
||||
@Override
|
||||
public void sessionIdle(IoSession session, IdleStatus status) {
|
||||
Log.d(TAG, "****************CIM "+status.toString().toUpperCase()+":"+session.getLocalAddress() +" NID:"+session.getId()+ " isConnected:" + session.isConnected());
|
||||
Log.d(TAG, "****************CIM " + status.toString().toUpperCase() + ":" + session.getLocalAddress() + " NID:"
|
||||
+ session.getId() + " isConnected:" + session.isConnected());
|
||||
|
||||
/**
|
||||
* 用于解决,wifi情况下。偶而路由器与服务器断开连接时,客户端并没及时收到关闭事件
|
||||
* 导致这样的情况下当前连接无效也不会重连的问题
|
||||
* 用于解决,wifi情况下。偶而路由器与服务器断开连接时,客户端并没及时收到关闭事件 导致这样的情况下当前连接无效也不会重连的问题
|
||||
*
|
||||
*/
|
||||
long lastHeartbeatTime = getLastHeartbeatTime(session);
|
||||
if(System.currentTimeMillis() - lastHeartbeatTime >= HEARBEAT_TIME_OUT)
|
||||
{
|
||||
if (System.currentTimeMillis() - lastHeartbeatTime >= HEARBEAT_TIME_OUT) {
|
||||
session.closeNow();
|
||||
Log.e(TAG, "****************CIM心跳超时 ,即将重新连接......" + " NID:" + session.getId());
|
||||
}
|
||||
@ -304,8 +291,7 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
|
||||
@Override
|
||||
public void messageSent(IoSession session, Object message) {
|
||||
if(message instanceof SentBody)
|
||||
{
|
||||
if (message instanceof SentBody) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED);
|
||||
intent.putExtra(SentBody.class.getName(), (SentBody) message);
|
||||
@ -313,14 +299,11 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setLastHeartbeatTime(IoSession session)
|
||||
{
|
||||
private void setLastHeartbeatTime(IoSession session) {
|
||||
session.setAttribute(KEY_LAST_HEART_TIME, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
private long getLastHeartbeatTime(IoSession session)
|
||||
{
|
||||
private long getLastHeartbeatTime(IoSession session) {
|
||||
long time = 0;
|
||||
Object value = session.getAttribute(KEY_LAST_HEART_TIME);
|
||||
if (value != null) {
|
||||
@ -334,12 +317,12 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
ConnectivityManager nw = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo networkInfo = nw.getActiveNetworkInfo();
|
||||
return networkInfo != null;
|
||||
} catch (Exception e) {}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getRequest(IoSession arg0) {
|
||||
return null;
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.android.exception.SessionClosedException;
|
||||
import com.farsunset.cim.sdk.android.model.Message;
|
||||
@ -33,6 +32,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
/**
|
||||
* 消息入口,所有消息都会经过这里
|
||||
*/
|
||||
@ -50,33 +50,30 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
*/
|
||||
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)
|
||||
|| intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)
|
||||
||intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED))
|
||||
{
|
||||
|| intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
|
||||
startPushService();
|
||||
}
|
||||
|
||||
/*
|
||||
* 设备网络状态变化事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED))
|
||||
{
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED)) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
onDevicesNetworkChanged(connectivityManager.getActiveNetworkInfo());
|
||||
}
|
||||
|
||||
/*
|
||||
* cim断开服务器事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED)) {
|
||||
onInnerConnectionClosed();
|
||||
}
|
||||
|
||||
/*
|
||||
* cim连接服务器失败事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED)) {
|
||||
long interval = intent.getLongExtra("interval", CIMConstant.RECONN_INTERVAL_TIME);
|
||||
String exceptionName = intent.getStringExtra(Exception.class.getName());
|
||||
onConnectionFailed(exceptionName, interval);
|
||||
@ -85,34 +82,28 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
/*
|
||||
* cim连接服务器成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_SUCCESSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_SUCCESSED)) {
|
||||
onInnerConnectionSuccessed();
|
||||
}
|
||||
|
||||
/*
|
||||
* 收到推送消息事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED)) {
|
||||
onInnerMessageReceived((Message) intent.getSerializableExtra(Message.class.getName()), intent);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取收到replybody成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED)) {
|
||||
onReplyReceived((ReplyBody) intent.getSerializableExtra(ReplyBody.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取sendbody发送失败事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_FAILED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_FAILED)) {
|
||||
String exceptionName = intent.getStringExtra(Exception.class.getName());
|
||||
SentBody sentBody = (SentBody) intent.getSerializableExtra(SentBody.class.getName());
|
||||
onSentFailed(exceptionName, sentBody);
|
||||
@ -121,18 +112,14 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
/*
|
||||
* 获取sendbody发送成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED)) {
|
||||
onSentSucceed((SentBody) intent.getSerializableExtra(SentBody.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 重新连接,如果断开的话
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
}
|
||||
@ -145,8 +132,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
|
||||
private void onInnerConnectionClosed() {
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE, false);
|
||||
if(CIMConnectorManager.isNetworkConnected(context))
|
||||
{
|
||||
if (CIMConnectorManager.isNetworkConnected(context)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
|
||||
@ -155,8 +141,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
|
||||
private void onConnectionFailed(String exceptionName, long reinterval) {
|
||||
|
||||
if(CIMConnectorManager.isNetworkConnected(context))
|
||||
{
|
||||
if (CIMConnectorManager.isNetworkConnected(context)) {
|
||||
onConnectionFailed();
|
||||
|
||||
CIMPushManager.connect(context, reinterval);
|
||||
@ -172,8 +157,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
|
||||
private void onDevicesNetworkChanged(NetworkInfo info) {
|
||||
|
||||
if(info !=null)
|
||||
{
|
||||
if (info != null) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
|
||||
@ -181,42 +165,35 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
}
|
||||
|
||||
private void onInnerMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent) {
|
||||
if(isForceOfflineMessage(message.getAction()))
|
||||
{
|
||||
if (isForceOfflineMessage(message.getAction())) {
|
||||
CIMPushManager.stop(context);
|
||||
}
|
||||
|
||||
onMessageReceived(message, intent);
|
||||
}
|
||||
|
||||
private boolean isForceOfflineMessage(String action)
|
||||
{
|
||||
private boolean isForceOfflineMessage(String action) {
|
||||
return CIMConstant.MessageAction.ACTION_999.equals(action);
|
||||
}
|
||||
|
||||
private void onSentFailed(String exceptionName, SentBody body) {
|
||||
|
||||
// 与服务端端开链接,重新连接
|
||||
if(SessionClosedException.class.getSimpleName().equals(exceptionName))
|
||||
{
|
||||
if (SessionClosedException.class.getSimpleName().equals(exceptionName)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
// 发送失败 重新发送
|
||||
CIMPushManager.sendRequest(context, body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public abstract void onMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent);
|
||||
|
||||
public void onNetworkChanged(NetworkInfo info) {
|
||||
CIMListenerManager.notifyOnNetworkChanged(info);
|
||||
}
|
||||
|
||||
|
||||
public void onConnectionSuccessed(boolean hasAutoBind) {
|
||||
CIMListenerManager.notifyOnConnectionSuccessed(hasAutoBind);
|
||||
}
|
||||
|
@ -30,39 +30,41 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
/**
|
||||
* CIM 主要事件接口
|
||||
*/
|
||||
public interface CIMEventListener
|
||||
{
|
||||
|
||||
public interface CIMEventListener {
|
||||
|
||||
/**
|
||||
* 当收到服务端推送过来的消息时调用
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
void onMessageReceived(Message message);
|
||||
|
||||
/**
|
||||
* 当调用CIMPushManager.sendRequest()向服务端发送请求,获得相应时调用
|
||||
*
|
||||
* @param replybody
|
||||
*/
|
||||
void onReplyReceived(ReplyBody replybody);
|
||||
|
||||
/**
|
||||
* 当调用CIMPushManager.sendRequest()向服务端发送请求成功时
|
||||
*
|
||||
* @param body
|
||||
*/
|
||||
void onSentSuccessed(SentBody body);
|
||||
|
||||
|
||||
/**
|
||||
* 当手机网络发生变化时调用
|
||||
*
|
||||
* @param networkinfo
|
||||
*/
|
||||
void onNetworkChanged(NetworkInfo networkinfo);
|
||||
|
||||
|
||||
/**
|
||||
* 当连接服务器成功时回调
|
||||
* @param hasAutoBind : true 已经自动绑定账号到服务器了,不需要再手动调用bindAccount
|
||||
*
|
||||
* @param hasAutoBind
|
||||
* : true 已经自动绑定账号到服务器了,不需要再手动调用bindAccount
|
||||
*/
|
||||
void onConnectionSuccessed(boolean hasAutoBind);
|
||||
|
||||
@ -83,4 +85,3 @@ public interface CIMEventListener
|
||||
*/
|
||||
int getEventDispatchOrder();
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@ -31,7 +32,6 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
import android.net.NetworkInfo;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
/**
|
||||
* CIM 消息监听器管理
|
||||
*/
|
||||
@ -48,7 +48,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void removeMessageListener(CIMEventListener listener) {
|
||||
for (int i = 0; i < cimListeners.size(); i++) {
|
||||
if (listener.getClass() == cimListeners.get(i).getClass()) {
|
||||
@ -57,19 +56,18 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void notifyOnNetworkChanged(NetworkInfo info) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onNetworkChanged(info);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void notifyOnConnectionSuccessed(boolean hasAutoBind) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onConnectionSuccessed(hasAutoBind);
|
||||
}
|
||||
}
|
||||
|
||||
public static void notifyOnMessageReceived(Message message) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onMessageReceived(message);
|
||||
@ -88,7 +86,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void notifyOnReplyReceived(ReplyBody body) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onReplyReceived(body);
|
||||
@ -101,7 +98,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void destory() {
|
||||
cimListeners.clear();
|
||||
}
|
||||
@ -127,5 +123,4 @@ public class CIMListenerManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import android.content.Context;
|
||||
@ -52,6 +53,7 @@ public class CIMPushManager {
|
||||
|
||||
/**
|
||||
* 初始化,连接服务端,在程序启动页或者 在Application里调用
|
||||
*
|
||||
* @param context
|
||||
* @param ip
|
||||
* @param port
|
||||
@ -70,8 +72,7 @@ public class CIMPushManager {
|
||||
CIMCacheManager.putString(context, CIMCacheManager.KEY_CIM_SERVIER_HOST, ip);
|
||||
CIMCacheManager.putInt(context, CIMCacheManager.KEY_CIM_SERVIER_PORT, port);
|
||||
|
||||
if(!autoBind)
|
||||
{
|
||||
if (!autoBind) {
|
||||
CIMCacheManager.remove(context, CIMCacheManager.KEY_ACCOUNT);
|
||||
}
|
||||
|
||||
@ -89,8 +90,7 @@ public class CIMPushManager {
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if(isManualStop || isManualDestory)
|
||||
{
|
||||
if (isManualStop || isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -101,17 +101,16 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置一个账号登录到服务端
|
||||
* @param account 用户唯一ID
|
||||
*
|
||||
* @param account
|
||||
* 用户唯一ID
|
||||
*/
|
||||
public static void bindAccount(Context context, String account) {
|
||||
|
||||
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if(isManualDestory || account==null || account.trim().length()==0)
|
||||
{
|
||||
if (isManualDestory || account == null || account.trim().length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -119,7 +118,6 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static void sendBindRequest(Context context, String account) {
|
||||
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, false);
|
||||
@ -147,8 +145,7 @@ public class CIMPushManager {
|
||||
|
||||
String account = CIMCacheManager.getString(context, CIMCacheManager.KEY_ACCOUNT);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if( account==null || account.trim().length()==0 || isManualDestory )
|
||||
{
|
||||
if (account == null || account.trim().length() == 0 || isManualDestory) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -157,10 +154,9 @@ public class CIMPushManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 发送一个CIM请求
|
||||
*
|
||||
* @param context
|
||||
* @body
|
||||
*/
|
||||
@ -169,8 +165,7 @@ public class CIMPushManager {
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if(isManualStop || isManualDestory)
|
||||
{
|
||||
if (isManualStop || isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -183,6 +178,7 @@ public class CIMPushManager {
|
||||
|
||||
/**
|
||||
* 停止接受推送,将会退出当前账号登录,端口与服务端的连接
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void stop(Context context) {
|
||||
@ -200,14 +196,13 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 完全销毁CIM,一般用于完全退出程序,调用resume将不能恢复
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void destroy(Context context) {
|
||||
|
||||
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED, true);
|
||||
CIMCacheManager.putString(context, CIMCacheManager.KEY_ACCOUNT, null);
|
||||
|
||||
@ -217,9 +212,9 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重新恢复接收推送,重新连接服务端,并登录当前账号
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void resume(Context context) {
|
||||
@ -236,8 +231,6 @@ public class CIMPushManager {
|
||||
return CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static String getVersionName(Context context) {
|
||||
String versionName = null;
|
||||
try {
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
@ -28,9 +29,9 @@ import android.os.Message;
|
||||
import android.util.Log;
|
||||
import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
|
||||
|
||||
/**
|
||||
* 与服务端连接服务
|
||||
*
|
||||
* @author 3979434
|
||||
*
|
||||
*/
|
||||
@ -38,13 +39,12 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
private final String TAG = CIMPushService.class.getSimpleName();
|
||||
public final static String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
|
||||
private CIMConnectorManager manager;
|
||||
|
||||
@Override
|
||||
public void onCreate()
|
||||
{
|
||||
public void onCreate() {
|
||||
manager = CIMConnectorManager.getManager(this.getApplicationContext());
|
||||
}
|
||||
|
||||
|
||||
Handler connectionHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(android.os.Message message) {
|
||||
@ -57,17 +57,14 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
|
||||
intent = (intent == null ? new Intent(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE) : intent);
|
||||
|
||||
String action = intent.getAction();
|
||||
|
||||
if(CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
|
||||
|
||||
long delayMillis = intent.getLongExtra(KEY_DELAYED_TIME, 0);
|
||||
if (delayMillis > 0) {
|
||||
@ -77,46 +74,40 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
msg.setData(intent.getExtras());
|
||||
connectionHandler.sendMessageDelayed(msg, delayMillis);
|
||||
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
String host = intent.getStringExtra(CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = intent.getIntExtra(CIMCacheManager.KEY_CIM_SERVIER_PORT, 0);
|
||||
manager.connect(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action)) {
|
||||
manager.send((SentBody) intent.getSerializableExtra(CIMPushManager.KEY_SEND_BODY));
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action)) {
|
||||
manager.closeSession();
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_DESTORY.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_DESTORY.equals(action)) {
|
||||
manager.destroy();
|
||||
this.stopSelf();
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action) )
|
||||
{
|
||||
if (CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action)) {
|
||||
if (!manager.isConnected()) {
|
||||
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(getApplicationContext(),CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(getApplicationContext(),
|
||||
CIMCacheManager.KEY_MANUAL_STOP);
|
||||
Log.w(TAG, "manager.isConnected() == false, isManualStop == " + isManualStop);
|
||||
CIMPushManager.connect(this, 0);
|
||||
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
Log.i(TAG, "manager.isConnected() == true");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ public interface CIMConstant {
|
||||
|
||||
// 消息头长度为3个字节,第一个字节为消息类型,第二,第三字节 转换int后为消息长度
|
||||
int DATA_HEADER_LENGTH = 3;
|
||||
|
||||
public static interface ReturnCode {
|
||||
|
||||
String CODE_404 = "404";
|
||||
@ -44,11 +45,8 @@ public interface CIMConstant {
|
||||
|
||||
String CODE_500 = "500";
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static interface ProtobufType {
|
||||
byte C_H_RS = 0;
|
||||
byte S_H_RQ = 1;
|
||||
@ -59,7 +57,6 @@ public interface CIMConstant {
|
||||
|
||||
public static interface RequestKey {
|
||||
|
||||
|
||||
String CLIENT_BIND = "client_bind";
|
||||
|
||||
String CLIENT_LOGOUT = "client_logout";
|
||||
@ -69,14 +66,12 @@ public interface CIMConstant {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static interface MessageAction {
|
||||
|
||||
// 被其他设备登录挤下线消息
|
||||
String ACTION_999 = "999";
|
||||
}
|
||||
|
||||
|
||||
public static interface IntentAction {
|
||||
|
||||
// 消息广播action
|
||||
|
@ -40,7 +40,6 @@ public class ClientMessageCodecFactory implements ProtocolCodecFactory {
|
||||
*/
|
||||
private final ClientMessageDecoder decoder;
|
||||
|
||||
|
||||
public ClientMessageCodecFactory() {
|
||||
encoder = new ClientMessageEncoder();
|
||||
decoder = new ClientMessageDecoder();
|
||||
@ -49,6 +48,7 @@ public class ClientMessageCodecFactory implements ProtocolCodecFactory {
|
||||
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
public ProtocolDecoder getDecoder(IoSession session) throws Exception {
|
||||
return decoder;
|
||||
}
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android.filter;
|
||||
|
||||
|
||||
import org.apache.mina.core.buffer.IoBuffer;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
|
||||
@ -35,6 +34,7 @@ import com.farsunset.cim.sdk.android.model.ReplyBody;
|
||||
import com.farsunset.cim.sdk.android.model.proto.MessageProto;
|
||||
import com.farsunset.cim.sdk.android.model.proto.ReplyBodyProto;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
|
||||
/**
|
||||
* 客户端消息解码
|
||||
*/
|
||||
@ -42,10 +42,8 @@ public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
|
||||
final static String TAG = ClientMessageDecoder.class.getSimpleName();
|
||||
|
||||
|
||||
@Override
|
||||
public boolean doDecode(IoSession iosession, IoBuffer iobuffer,
|
||||
ProtocolDecoderOutput out) throws Exception {
|
||||
public boolean doDecode(IoSession iosession, IoBuffer iobuffer, ProtocolDecoderOutput out) throws Exception {
|
||||
|
||||
/**
|
||||
* 消息头3位
|
||||
@ -82,17 +80,13 @@ public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
|
||||
private Object mappingMessageObject(byte[] bytes, byte type) throws InvalidProtocolBufferException {
|
||||
|
||||
|
||||
|
||||
if(CIMConstant.ProtobufType.S_H_RQ == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.S_H_RQ == type) {
|
||||
HeartbeatRequest request = HeartbeatRequest.getInstance();
|
||||
Log.i(TAG, request.toString());
|
||||
return request;
|
||||
}
|
||||
|
||||
if(CIMConstant.ProtobufType.REPLYBODY == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.REPLYBODY == type) {
|
||||
ReplyBodyProto.Model bodyProto = ReplyBodyProto.Model.parseFrom(bytes);
|
||||
ReplyBody body = new ReplyBody();
|
||||
body.setKey(bodyProto.getKey());
|
||||
@ -106,8 +100,7 @@ public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
return body;
|
||||
}
|
||||
|
||||
if(CIMConstant.ProtobufType.MESSAGE == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.MESSAGE == type) {
|
||||
MessageProto.Model bodyProto = MessageProto.Model.parseFrom(bytes);
|
||||
Message message = new Message();
|
||||
message.setMid(bodyProto.getMid());
|
||||
@ -130,6 +123,7 @@ public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
|
||||
/**
|
||||
* 解析消息体长度
|
||||
*
|
||||
* @param type
|
||||
* @param length
|
||||
* @return
|
||||
|
@ -29,12 +29,14 @@ import android.util.Log;
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.android.model.Protobufable;
|
||||
|
||||
/**
|
||||
* 客户端消息发送前进行编码
|
||||
*/
|
||||
public class ClientMessageEncoder extends ProtocolEncoderAdapter {
|
||||
|
||||
final static String TAG = ClientMessageEncoder.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
public void encode(IoSession iosession, Object object, ProtocolEncoderOutput out) throws Exception {
|
||||
|
||||
@ -58,6 +60,7 @@ public class ClientMessageEncoder extends ProtocolEncoderAdapter {
|
||||
|
||||
/**
|
||||
* 消息体最大为65535
|
||||
*
|
||||
* @param type
|
||||
* @param length
|
||||
* @return
|
||||
|
@ -24,6 +24,7 @@ package com.farsunset.cim.sdk.android.model;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
|
||||
/**
|
||||
* 服务端心跳请求
|
||||
*
|
||||
@ -53,7 +54,6 @@ public class HeartbeatRequest implements Serializable,Protobufable {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte getType() {
|
||||
return CIMConstant.ProtobufType.S_H_RQ;
|
||||
|
@ -24,6 +24,7 @@ package com.farsunset.cim.sdk.android.model;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
|
||||
/**
|
||||
* 客户端心跳响应
|
||||
*/
|
||||
@ -42,6 +43,7 @@ public class HeartbeatResponse implements Serializable,Protobufable {
|
||||
public static HeartbeatResponse getInstance() {
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getByteArray() {
|
||||
return CMD_HEARTBEAT_RESPONSE.getBytes();
|
||||
|
@ -30,13 +30,11 @@ public class Message implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
private String mid;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
@ -59,8 +57,6 @@ public class Message implements Serializable {
|
||||
*/
|
||||
private String receiver;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* content 内容格式
|
||||
*/
|
||||
@ -73,11 +69,10 @@ public class Message implements Serializable {
|
||||
|
||||
private long timestamp;
|
||||
|
||||
|
||||
public Message()
|
||||
{
|
||||
public Message() {
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -86,14 +81,14 @@ public class Message implements Serializable {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@ -126,7 +121,6 @@ public class Message implements Serializable {
|
||||
this.receiver = receiver;
|
||||
}
|
||||
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
@ -135,14 +129,14 @@ public class Message implements Serializable {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getExtra() {
|
||||
return extra;
|
||||
}
|
||||
|
||||
public void setExtra(String extra) {
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
@ -159,7 +153,6 @@ public class Message implements Serializable {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
public String getMid() {
|
||||
return mid;
|
||||
}
|
||||
@ -172,5 +165,4 @@ public class Message implements Serializable {
|
||||
return txt != null && txt.trim().length() != 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android.model;
|
||||
|
||||
/**
|
||||
* 需要向另一端发送的结构体
|
||||
*/
|
||||
|
@ -39,7 +39,6 @@ public class ReplyBody implements Serializable {
|
||||
*/
|
||||
private String key;
|
||||
|
||||
|
||||
/**
|
||||
* 返回码
|
||||
*/
|
||||
@ -50,16 +49,13 @@ public class ReplyBody implements Serializable {
|
||||
*/
|
||||
private String message;
|
||||
|
||||
|
||||
private long timestamp;
|
||||
|
||||
|
||||
/**
|
||||
* 返回数据集合
|
||||
*/
|
||||
private Hashtable<String, String> data = new Hashtable<String, String>();
|
||||
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -68,8 +64,6 @@ public class ReplyBody implements Serializable {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
@ -114,9 +108,7 @@ public class ReplyBody implements Serializable {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#ReplyBody#").append("\n");
|
||||
buffer.append("key:").append(this.getKey()).append("\n");
|
||||
@ -125,8 +117,7 @@ public class ReplyBody implements Serializable {
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
|
@ -73,12 +73,10 @@ public class SentBody implements Serializable,Protobufable {
|
||||
data.put(k, v);
|
||||
}
|
||||
|
||||
|
||||
public Set<String> getKeySet() {
|
||||
return data.keySet();
|
||||
}
|
||||
|
||||
|
||||
public void remove(String k) {
|
||||
data.remove(k);
|
||||
}
|
||||
@ -86,13 +84,13 @@ public class SentBody implements Serializable,Protobufable {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#SentBody#").append("\n");;
|
||||
buffer.append("#SentBody#").append("\n");
|
||||
;
|
||||
buffer.append("key:").append(key).append("\n");
|
||||
buffer.append("timestamp:").append(timestamp).append("\n");
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -23,7 +23,7 @@ package com.farsunset.cim.sdk.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
class CIMCacheToolkit {
|
||||
class CIMCacheManager {
|
||||
|
||||
private static HashMap<String, String> CIM_CONFIG_INFO = new HashMap<String, String>();
|
||||
|
||||
@ -37,53 +37,42 @@ class CIMCacheToolkit {
|
||||
|
||||
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
|
||||
|
||||
static CIMCacheToolkit toolkit;
|
||||
public static CIMCacheToolkit getInstance(){
|
||||
static CIMCacheManager toolkit;
|
||||
|
||||
public static CIMCacheManager getInstance() {
|
||||
if (toolkit == null) {
|
||||
toolkit = new CIMCacheToolkit();
|
||||
toolkit = new CIMCacheManager();
|
||||
}
|
||||
return toolkit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void remove(String key)
|
||||
{
|
||||
public void remove(String key) {
|
||||
CIM_CONFIG_INFO.remove(key);
|
||||
}
|
||||
|
||||
|
||||
public void putString(String key,String value)
|
||||
{
|
||||
public void putString(String key, String value) {
|
||||
CIM_CONFIG_INFO.put(key, value);
|
||||
|
||||
}
|
||||
|
||||
public String getString(String key)
|
||||
{
|
||||
public String getString(String key) {
|
||||
return CIM_CONFIG_INFO.get(key);
|
||||
}
|
||||
|
||||
public void putBoolean(String key,boolean value)
|
||||
{
|
||||
public void putBoolean(String key, boolean value) {
|
||||
putString(key, Boolean.toString(value));
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key)
|
||||
{
|
||||
public boolean getBoolean(String key) {
|
||||
String value = getString(key);
|
||||
return value == null ? false : Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
|
||||
public void putInt(String key,int value)
|
||||
{
|
||||
public void putInt(String key, int value) {
|
||||
putString(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
public int getInt(String key)
|
||||
{
|
||||
public int getInt(String key) {
|
||||
String value = getString(key);
|
||||
return value == null ? 0 : Integer.parseInt(value);
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
@ -47,7 +48,6 @@ import com.farsunset.cim.sdk.client.model.Message;
|
||||
import com.farsunset.cim.sdk.client.model.ReplyBody;
|
||||
import com.farsunset.cim.sdk.client.model.SentBody;
|
||||
|
||||
|
||||
/**
|
||||
* 连接服务端管理,cim核心处理类,管理连接,以及消息处理
|
||||
*
|
||||
@ -70,8 +70,6 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
|
||||
private static CIMConnectorManager manager;
|
||||
|
||||
|
||||
|
||||
private CIMConnectorManager() {
|
||||
connector = new NioSocketConnector();
|
||||
connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
|
||||
@ -109,7 +107,7 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
|
||||
logger.info("****************CIM正在连接服务器 " + host + ":" + port + "......");
|
||||
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_CIM_CONNECTION_STATE, false);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_CIM_CONNECTION_STATE, false);
|
||||
InetSocketAddress remoteSocketAddress = new InetSocketAddress(host, port);
|
||||
connectFuture = connector.connect(remoteSocketAddress);
|
||||
connectFuture.awaitUninterruptibly();
|
||||
@ -124,7 +122,8 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
intent.putExtra("interval", interval);
|
||||
sendBroadcast(intent);
|
||||
|
||||
logger.error("****************CIM连接服务器失败 "+host+":"+port+"......将在"+interval/1000+"秒后重新尝试连接");
|
||||
logger.error(
|
||||
"****************CIM连接服务器失败 " + host + ":" + port + "......将在" + interval / 1000 + "秒后重新尝试连接");
|
||||
|
||||
}
|
||||
|
||||
@ -133,8 +132,7 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
public void connect(final String host, final int port) {
|
||||
|
||||
executor.execute(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
public void run() {
|
||||
syncConnection(host, port);
|
||||
}
|
||||
});
|
||||
@ -148,8 +146,7 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
Throwable exception = new SessionDisconnectedException();
|
||||
|
||||
IoSession session = getCurrentSession();
|
||||
if(session!=null && session.isConnected())
|
||||
{
|
||||
if (session != null && session.isConnected()) {
|
||||
WriteFuture wf = session.write(body);
|
||||
// 消息发送超时 5秒
|
||||
wf.awaitUninterruptibly(WRITE_TIMEOUT);
|
||||
@ -185,22 +182,16 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
return session != null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void closeSession()
|
||||
{
|
||||
public void closeSession() {
|
||||
IoSession session = getCurrentSession();
|
||||
if(session!=null)
|
||||
{
|
||||
if (session != null) {
|
||||
session.closeNow();
|
||||
}
|
||||
}
|
||||
|
||||
public IoSession getCurrentSession()
|
||||
{
|
||||
public IoSession getCurrentSession() {
|
||||
Map<Long, IoSession> sessions = connector.getManagedSessions();
|
||||
for(Long key:sessions.keySet())
|
||||
{
|
||||
for (Long key : sessions.keySet()) {
|
||||
IoSession session = sessions.get(key);
|
||||
if (session.isConnected()) {
|
||||
return session;
|
||||
@ -209,11 +200,9 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sessionCreated(IoSession session) throws Exception {
|
||||
|
||||
|
||||
logger.info("****************CIM连接服务器成功:" + session.getLocalAddress() + " NID:" + session.getId());
|
||||
|
||||
setLastHeartbeatTime(session);
|
||||
@ -237,16 +226,15 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
|
||||
@Override
|
||||
public void sessionIdle(IoSession session, IdleStatus status) {
|
||||
logger.debug("****************CIM "+status.toString().toUpperCase()+":"+session.getLocalAddress() +" NID:"+session.getId()+ " isConnected:" + session.isConnected());
|
||||
logger.debug("****************CIM " + status.toString().toUpperCase() + ":" + session.getLocalAddress()
|
||||
+ " NID:" + session.getId() + " isConnected:" + session.isConnected());
|
||||
|
||||
/**
|
||||
* 用于解决,wifi情况下。偶而路由器与服务器断开连接时,客户端并没及时收到关闭事件
|
||||
* 导致这样的情况下当前连接无效也不会重连的问题
|
||||
* 用于解决,wifi情况下。偶而路由器与服务器断开连接时,客户端并没及时收到关闭事件 导致这样的情况下当前连接无效也不会重连的问题
|
||||
*
|
||||
*/
|
||||
long lastHeartbeatTime = getLastHeartbeatTime(session);
|
||||
if(System.currentTimeMillis() - lastHeartbeatTime >= HEARBEAT_TIME_OUT)
|
||||
{
|
||||
if (System.currentTimeMillis() - lastHeartbeatTime >= HEARBEAT_TIME_OUT) {
|
||||
session.closeNow();
|
||||
logger.error("****************CIM心跳超时 ,即将重新连接......" + " NID:" + session.getId());
|
||||
}
|
||||
@ -289,8 +277,7 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
|
||||
@Override
|
||||
public void messageSent(IoSession session, Object message) {
|
||||
if(message instanceof SentBody)
|
||||
{
|
||||
if (message instanceof SentBody) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED);
|
||||
intent.putExtra(SentBody.class.getName(), (SentBody) message);
|
||||
@ -298,14 +285,11 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setLastHeartbeatTime(IoSession session)
|
||||
{
|
||||
private void setLastHeartbeatTime(IoSession session) {
|
||||
session.setAttribute(KEY_LAST_HEART_TIME, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
private long getLastHeartbeatTime(IoSession session)
|
||||
{
|
||||
private long getLastHeartbeatTime(IoSession session) {
|
||||
long time = 0;
|
||||
Object value = session.getAttribute(KEY_LAST_HEART_TIME);
|
||||
if (value != null) {
|
||||
@ -314,7 +298,6 @@ class CIMConnectorManager extends IoHandlerAdapter implements KeepAliveMessageF
|
||||
return time;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getRequest(IoSession arg0) {
|
||||
return null;
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client;
|
||||
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
@ -41,6 +40,7 @@ public class CIMEventBroadcastReceiver {
|
||||
private static CIMEventBroadcastReceiver recerver;
|
||||
private CIMEventListener listener;
|
||||
private Timer connectionHandler = new Timer();;
|
||||
|
||||
public static CIMEventBroadcastReceiver getInstance() {
|
||||
if (recerver == null) {
|
||||
recerver = new CIMEventBroadcastReceiver();
|
||||
@ -52,23 +52,19 @@ public class CIMEventBroadcastReceiver {
|
||||
listener = ls;
|
||||
}
|
||||
|
||||
|
||||
public void onReceive(Intent intent) {
|
||||
|
||||
|
||||
/*
|
||||
* cim断开服务器事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED)) {
|
||||
onInnerConnectionClosed();
|
||||
}
|
||||
|
||||
/*
|
||||
* cim连接服务器失败事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED)) {
|
||||
long interval = intent.getLongExtra("interval", CIMConstant.RECONN_INTERVAL_TIME);
|
||||
onInnerConnectionFailed((Exception) intent.getExtra(Exception.class.getName()), interval);
|
||||
}
|
||||
@ -76,78 +72,63 @@ public class CIMEventBroadcastReceiver {
|
||||
/*
|
||||
* cim连接服务器成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_SUCCESSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_SUCCESSED)) {
|
||||
onInnerConnectionSuccessed();
|
||||
}
|
||||
|
||||
/*
|
||||
* 收到推送消息事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED)) {
|
||||
onInnerMessageReceived((Message) intent.getExtra(Message.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取收到replybody成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED)) {
|
||||
listener.onReplyReceived((ReplyBody) intent.getExtra(ReplyBody.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取sendbody发送失败事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_FAILED))
|
||||
{
|
||||
onSentFailed((Exception) intent.getExtra(Exception.class.getName()),(SentBody)intent.getExtra(SentBody.class.getName()));
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_FAILED)) {
|
||||
onSentFailed((Exception) intent.getExtra(Exception.class.getName()),
|
||||
(SentBody) intent.getExtra(SentBody.class.getName()));
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取sendbody发送成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED)) {
|
||||
onSentSucceed((SentBody) intent.getExtra(SentBody.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取cim数据传输异常事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_UNCAUGHT_EXCEPTION))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_UNCAUGHT_EXCEPTION)) {
|
||||
onUncaughtException((Exception) intent.getExtra(Exception.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 重新连接,如果断开的话
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY)) {
|
||||
CIMPushManager.connect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void onInnerConnectionClosed() {
|
||||
|
||||
listener.onConnectionClosed();
|
||||
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_CIM_CONNECTION_STATE, false);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_CIM_CONNECTION_STATE, false);
|
||||
CIMPushManager.connect();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void onInnerConnectionFailed(Exception e, long interval) {
|
||||
|
||||
connectionHandler.schedule(new ConnectionTask(), interval);
|
||||
@ -156,41 +137,43 @@ public class CIMEventBroadcastReceiver {
|
||||
}
|
||||
|
||||
private void onInnerConnectionSuccessed() {
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_CIM_CONNECTION_STATE, true);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_CIM_CONNECTION_STATE, true);
|
||||
|
||||
boolean autoBind = CIMPushManager.autoBindDeviceId();
|
||||
|
||||
listener.onConnectionSuccessed(autoBind);
|
||||
}
|
||||
|
||||
private void onUncaughtException(Throwable arg0) {}
|
||||
private void onUncaughtException(Throwable arg0) {
|
||||
}
|
||||
|
||||
private void onInnerMessageReceived(com.farsunset.cim.sdk.client.model.Message message) {
|
||||
if (isForceOfflineMessage(message.getAction())) {
|
||||
CIMPushManager.stop();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void onInnerMessageReceived(com.farsunset.cim.sdk.client.model.Message message)
|
||||
{
|
||||
listener.onMessageReceived(message);
|
||||
}
|
||||
|
||||
private boolean isForceOfflineMessage(String action) {
|
||||
return CIMConstant.MessageAction.ACTION_999.equals(action);
|
||||
}
|
||||
|
||||
private void onSentFailed(Exception e, SentBody body) {
|
||||
|
||||
e.printStackTrace();
|
||||
// 与服务端端开链接,重新连接
|
||||
if(e instanceof SessionDisconnectedException)
|
||||
{
|
||||
if (e instanceof SessionDisconnectedException) {
|
||||
CIMPushManager.connect();
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
// 发送失败 重新发送
|
||||
//CIMPushManager.sendRequest( body);
|
||||
CIMPushManager.sendRequest(body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void onSentSucceed(SentBody body){}
|
||||
|
||||
|
||||
private void onSentSucceed(SentBody body) {
|
||||
}
|
||||
|
||||
class ConnectionTask extends TimerTask {
|
||||
|
||||
|
@ -21,33 +21,33 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client;
|
||||
|
||||
|
||||
import com.farsunset.cim.sdk.client.model.Message;
|
||||
import com.farsunset.cim.sdk.client.model.ReplyBody;
|
||||
|
||||
/**
|
||||
* CIM 主要事件接口
|
||||
*/
|
||||
public interface CIMEventListener
|
||||
{
|
||||
|
||||
public interface CIMEventListener {
|
||||
|
||||
/**
|
||||
* 当收到服务端推送过来的消息时调用
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
void onMessageReceived(Message message);
|
||||
|
||||
/**
|
||||
* 当调用CIMPushManager.sendRequest()向服务端发送请求,获得相应时调用
|
||||
*
|
||||
* @param replybody
|
||||
*/
|
||||
void onReplyReceived(ReplyBody replybody);
|
||||
|
||||
|
||||
/**
|
||||
* 当连接服务器成功时回调
|
||||
* @param hasAutoBind : true 已经自动绑定账号到服务器了,不需要再手动调用bindAccount
|
||||
*
|
||||
* @param hasAutoBind
|
||||
* : true 已经自动绑定账号到服务器了,不需要再手动调用bindAccount
|
||||
*/
|
||||
void onConnectionSuccessed(boolean hasAutoBind);
|
||||
|
||||
@ -67,4 +67,3 @@ public interface CIMEventListener
|
||||
*/
|
||||
int getEventDispatchOrder();
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@ -29,7 +30,6 @@ import org.apache.log4j.Logger;
|
||||
import com.farsunset.cim.sdk.client.model.Message;
|
||||
import com.farsunset.cim.sdk.client.model.ReplyBody;
|
||||
|
||||
|
||||
/**
|
||||
* CIM 消息监听器管理
|
||||
*/
|
||||
@ -39,7 +39,6 @@ public class CIMListenerManager {
|
||||
private static CIMMessageReceiveComparator comparator = new CIMMessageReceiveComparator();
|
||||
protected static final Logger logger = Logger.getLogger(CIMListenerManager.class);
|
||||
|
||||
|
||||
public static void registerMessageListener(CIMEventListener listener) {
|
||||
|
||||
if (!cimListeners.contains(listener)) {
|
||||
@ -48,7 +47,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void removeMessageListener(CIMEventListener listener) {
|
||||
for (int i = 0; i < cimListeners.size(); i++) {
|
||||
if (listener.getClass() == cimListeners.get(i).getClass()) {
|
||||
@ -62,6 +60,7 @@ public class CIMListenerManager {
|
||||
listener.onConnectionSuccessed(antoBind);
|
||||
}
|
||||
}
|
||||
|
||||
public static void notifyOnMessageReceived(Message message) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onMessageReceived(message);
|
||||
@ -74,7 +73,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void notifyOnReplyReceived(ReplyBody body) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onReplyReceived(body);
|
||||
@ -112,5 +110,4 @@ public class CIMListenerManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Properties;
|
||||
@ -45,7 +46,6 @@ public class CIMPushManager {
|
||||
|
||||
static String ACTION_DESTORY = "ACTION_DESTORY";
|
||||
|
||||
|
||||
static String KEY_CIM_CONNECTION_STATUS = "KEY_CIM_CONNECTION_STATUS";
|
||||
|
||||
// 被销毁的destroy()
|
||||
@ -54,8 +54,10 @@ public class CIMPushManager {
|
||||
public static final int STATE_STOPED = 0x0000EE;
|
||||
|
||||
public static final int STATE_NORMAL = 0x000000;
|
||||
|
||||
/**
|
||||
* 初始化,连接服务端,在程序启动页或者 在Application里调用
|
||||
*
|
||||
* @param context
|
||||
* @param ip
|
||||
* @param port
|
||||
@ -63,15 +65,15 @@ public class CIMPushManager {
|
||||
|
||||
public static void connect(String ip, int port) {
|
||||
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED, false);
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_MANUAL_STOP, false);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_CIM_DESTROYED, false);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_MANUAL_STOP, false);
|
||||
|
||||
CIMCacheToolkit.getInstance().putString( CIMCacheToolkit.KEY_CIM_SERVIER_HOST, ip);
|
||||
CIMCacheToolkit.getInstance().putInt( CIMCacheToolkit.KEY_CIM_SERVIER_PORT, port);
|
||||
CIMCacheManager.getInstance().putString(CIMCacheManager.KEY_CIM_SERVIER_HOST, ip);
|
||||
CIMCacheManager.getInstance().putInt(CIMCacheManager.KEY_CIM_SERVIER_PORT, port);
|
||||
|
||||
Intent serviceIntent = new Intent();
|
||||
serviceIntent.putExtra(CIMCacheToolkit.KEY_CIM_SERVIER_HOST, ip);
|
||||
serviceIntent.putExtra(CIMCacheToolkit.KEY_CIM_SERVIER_PORT, port);
|
||||
serviceIntent.putExtra(CIMCacheManager.KEY_CIM_SERVIER_HOST, ip);
|
||||
serviceIntent.putExtra(CIMCacheManager.KEY_CIM_SERVIER_PORT, port);
|
||||
serviceIntent.setAction(ACTION_CREATE_CIM_CONNECTION);
|
||||
startService(serviceIntent);
|
||||
|
||||
@ -83,25 +85,23 @@ public class CIMPushManager {
|
||||
|
||||
protected static void connect() {
|
||||
|
||||
boolean isManualStop = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED);
|
||||
boolean isManualStop = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if(isManualStop || isManualDestory)
|
||||
{
|
||||
if (isManualStop || isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
String host = CIMCacheToolkit.getInstance().getString( CIMCacheToolkit.KEY_CIM_SERVIER_HOST);
|
||||
int port =CIMCacheToolkit.getInstance().getInt( CIMCacheToolkit.KEY_CIM_SERVIER_PORT);
|
||||
String host = CIMCacheManager.getInstance().getString(CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = CIMCacheManager.getInstance().getInt(CIMCacheManager.KEY_CIM_SERVIER_PORT);
|
||||
|
||||
connect(host, port);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static void sendBindRequest(String account) {
|
||||
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_MANUAL_STOP, false);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_MANUAL_STOP, false);
|
||||
SentBody sent = new SentBody();
|
||||
Properties sysPro = System.getProperties();
|
||||
sent.setKey(CIMConstant.RequestKey.CLIENT_BIND);
|
||||
@ -116,14 +116,14 @@ public class CIMPushManager {
|
||||
|
||||
/**
|
||||
* 设置一个账号登录到服务端
|
||||
* @param account 用户唯一ID
|
||||
*
|
||||
* @param account
|
||||
* 用户唯一ID
|
||||
*/
|
||||
public static void bindAccount(String account) {
|
||||
|
||||
|
||||
boolean isManualDestory = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED);
|
||||
if(isManualDestory || account==null || account.trim().length()==0)
|
||||
{
|
||||
boolean isManualDestory = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (isManualDestory || account == null || account.trim().length() == 0) {
|
||||
return;
|
||||
}
|
||||
sendBindRequest(account);
|
||||
@ -134,14 +134,9 @@ public class CIMPushManager {
|
||||
|
||||
String account = getAccount();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
boolean isManualDestory = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED);
|
||||
boolean isManualStoped = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_MANUAL_STOP);
|
||||
if( isManualStoped || account==null || account.trim().length()==0 || isManualDestory )
|
||||
{
|
||||
boolean isManualDestory = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
boolean isManualStoped = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
|
||||
if (isManualStoped || account == null || account.trim().length() == 0 || isManualDestory) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -150,20 +145,18 @@ public class CIMPushManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 发送一个CIM请求
|
||||
*
|
||||
* @param context
|
||||
* @body
|
||||
*/
|
||||
public static void sendRequest(SentBody body) {
|
||||
|
||||
boolean isManualStop = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED);
|
||||
boolean isManualStop = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if(isManualStop || isManualDestory)
|
||||
{
|
||||
if (isManualStop || isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -176,32 +169,30 @@ public class CIMPushManager {
|
||||
|
||||
/**
|
||||
* 停止接受推送,将会退出当前账号登录,端口与服务端的连接
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void stop() {
|
||||
|
||||
boolean isManualDestory = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED);
|
||||
boolean isManualDestory = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_MANUAL_STOP, true);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_MANUAL_STOP, true);
|
||||
|
||||
Intent serviceIntent = new Intent();
|
||||
serviceIntent.setAction(ACTION_CLOSE_CIM_CONNECTION);
|
||||
startService(serviceIntent);
|
||||
startService(new Intent(ACTION_CLOSE_CIM_CONNECTION));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 完全销毁CIM,一般用于完全退出程序,调用resume将不能恢复
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void destroy() {
|
||||
|
||||
|
||||
CIMCacheToolkit.getInstance().putBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED, true);
|
||||
CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_CIM_DESTROYED, true);
|
||||
|
||||
Intent serviceIntent = new Intent();
|
||||
serviceIntent.setAction(ACTION_DESTORY);
|
||||
@ -209,15 +200,15 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重新恢复接收推送,重新连接服务端,并登录当前账号如果aotuBind == true
|
||||
*
|
||||
* @param context
|
||||
* @param aotuBind
|
||||
*/
|
||||
public static void resume() {
|
||||
|
||||
boolean isManualDestory = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED);
|
||||
boolean isManualDestory = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (isManualDestory) {
|
||||
return;
|
||||
}
|
||||
@ -226,16 +217,16 @@ public class CIMPushManager {
|
||||
}
|
||||
|
||||
public static boolean isConnected() {
|
||||
return CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_CONNECTION_STATE);
|
||||
return CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_CONNECTION_STATE);
|
||||
}
|
||||
|
||||
public static int getState() {
|
||||
boolean isManualDestory = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_CIM_DESTROYED);
|
||||
boolean isManualDestory = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (isManualDestory) {
|
||||
return STATE_DESTROYED;
|
||||
}
|
||||
|
||||
boolean isManualStop = CIMCacheToolkit.getInstance().getBoolean(CIMCacheToolkit.KEY_MANUAL_STOP);
|
||||
boolean isManualStop = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
|
||||
if (isManualStop) {
|
||||
return STATE_STOPED;
|
||||
}
|
||||
@ -243,7 +234,6 @@ public class CIMPushManager {
|
||||
return STATE_NORMAL;
|
||||
}
|
||||
|
||||
|
||||
public static String getDeviceModel() {
|
||||
return System.getProperties().getProperty(CIMConstant.ConfigKey.DEVICE_MODEL);
|
||||
}
|
||||
@ -251,12 +241,15 @@ public class CIMPushManager {
|
||||
public static String getClientVersion() {
|
||||
return System.getProperties().getProperty(CIMConstant.ConfigKey.CLIENT_VERSION);
|
||||
}
|
||||
|
||||
public static String getAccount() {
|
||||
return System.getProperties().getProperty(CIMConstant.ConfigKey.CLIENT_ACCOUNT);
|
||||
}
|
||||
|
||||
public static void setAccount(String account) {
|
||||
System.getProperties().put(CIMConstant.ConfigKey.CLIENT_ACCOUNT, account);
|
||||
}
|
||||
|
||||
public static void setClientVersion(String version) {
|
||||
System.getProperties().put(CIMConstant.ConfigKey.CLIENT_VERSION, version);
|
||||
}
|
||||
|
@ -21,11 +21,9 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client;
|
||||
|
||||
|
||||
import com.farsunset.cim.sdk.client.model.Intent;
|
||||
import com.farsunset.cim.sdk.client.model.SentBody;
|
||||
|
||||
|
||||
/**
|
||||
* 与服务端连接服务
|
||||
*
|
||||
@ -36,6 +34,7 @@ import com.farsunset.cim.sdk.client.model.SentBody;
|
||||
private CIMConnectorManager manager;
|
||||
|
||||
private static CIMPushService service;
|
||||
|
||||
public static CIMPushService getInstance() {
|
||||
if (service == null) {
|
||||
service = new CIMPushService();
|
||||
@ -43,50 +42,40 @@ import com.farsunset.cim.sdk.client.model.SentBody;
|
||||
return service;
|
||||
}
|
||||
|
||||
|
||||
public CIMPushService()
|
||||
{
|
||||
public CIMPushService() {
|
||||
manager = CIMConnectorManager.getManager();
|
||||
}
|
||||
|
||||
|
||||
public void onStartCommand(Intent intent) {
|
||||
|
||||
intent = (intent == null ? new Intent(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE) : intent);
|
||||
|
||||
|
||||
String action = intent.getAction();
|
||||
|
||||
if(CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action))
|
||||
{
|
||||
String host = CIMCacheToolkit.getInstance().getString(CIMCacheToolkit.KEY_CIM_SERVIER_HOST);
|
||||
int port =CIMCacheToolkit.getInstance().getInt(CIMCacheToolkit.KEY_CIM_SERVIER_PORT);
|
||||
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
|
||||
String host = CIMCacheManager.getInstance().getString(CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = CIMCacheManager.getInstance().getInt(CIMCacheManager.KEY_CIM_SERVIER_PORT);
|
||||
manager.connect(host, port);
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action)) {
|
||||
manager.send((SentBody) intent.getExtra(SentBody.class.getName()));
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action)) {
|
||||
manager.closeSession();
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_DESTORY.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_DESTORY.equals(action)) {
|
||||
manager.destroy();
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action) && !manager.isConnected())
|
||||
{
|
||||
if (CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action) && !manager.isConnected()) {
|
||||
|
||||
String host = CIMCacheToolkit.getInstance().getString(CIMCacheToolkit.KEY_CIM_SERVIER_HOST);
|
||||
int port =CIMCacheToolkit.getInstance().getInt( CIMCacheToolkit.KEY_CIM_SERVIER_PORT);
|
||||
String host = CIMCacheManager.getInstance().getString(CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = CIMCacheManager.getInstance().getInt(CIMCacheManager.KEY_CIM_SERVIER_PORT);
|
||||
manager.connect(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ public interface CIMConstant {
|
||||
long RECONN_INTERVAL_TIME = 30 * 1000;
|
||||
// 消息头长度为3个字节,第一个字节为消息类型,第二,第三字节 转换int后为消息长度
|
||||
int DATA_HEADER_LENGTH = 3;
|
||||
|
||||
public static interface ReturnCode {
|
||||
|
||||
String CODE_404 = "404";
|
||||
@ -43,7 +44,6 @@ public interface CIMConstant {
|
||||
|
||||
String CODE_500 = "500";
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static interface ConfigKey {
|
||||
@ -64,7 +64,6 @@ public interface CIMConstant {
|
||||
|
||||
public static interface RequestKey {
|
||||
|
||||
|
||||
String CLIENT_BIND = "client_bind";
|
||||
|
||||
String CLIENT_LOGOUT = "client_logout";
|
||||
@ -74,7 +73,6 @@ public interface CIMConstant {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static interface MessageAction {
|
||||
|
||||
// 被其他设备登录挤下线消息
|
||||
@ -83,7 +81,6 @@ public interface CIMConstant {
|
||||
String ACTION_444 = "444";
|
||||
}
|
||||
|
||||
|
||||
public static interface IntentAction {
|
||||
|
||||
// 消息广播action
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client.exception;
|
||||
|
||||
|
||||
public class NetworkDisconnectedException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client.exception;
|
||||
|
||||
|
||||
public class SessionDisconnectedException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
@ -40,7 +40,6 @@ public class ClientMessageCodecFactory implements ProtocolCodecFactory {
|
||||
*/
|
||||
private final ClientMessageDecoder decoder;
|
||||
|
||||
|
||||
public ClientMessageCodecFactory() {
|
||||
encoder = new ClientMessageEncoder();
|
||||
decoder = new ClientMessageDecoder();
|
||||
@ -49,6 +48,7 @@ public class ClientMessageCodecFactory implements ProtocolCodecFactory {
|
||||
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
public ProtocolDecoder getDecoder(IoSession session) throws Exception {
|
||||
return decoder;
|
||||
}
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client.filter;
|
||||
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.mina.core.buffer.IoBuffer;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
@ -35,16 +34,15 @@ import com.farsunset.cim.sdk.client.model.ReplyBody;
|
||||
import com.farsunset.cim.sdk.model.proto.MessageProto;
|
||||
import com.farsunset.cim.sdk.model.proto.ReplyBodyProto;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
|
||||
/**
|
||||
* 客户端消息解码
|
||||
*/
|
||||
public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
protected final Logger logger = Logger.getLogger(ClientMessageDecoder.class);
|
||||
|
||||
|
||||
@Override
|
||||
public boolean doDecode(IoSession iosession, IoBuffer iobuffer,
|
||||
ProtocolDecoderOutput out) throws Exception {
|
||||
public boolean doDecode(IoSession iosession, IoBuffer iobuffer, ProtocolDecoderOutput out) throws Exception {
|
||||
|
||||
/**
|
||||
* 消息头3位
|
||||
@ -81,17 +79,13 @@ public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
|
||||
private Object mappingMessageObject(byte[] bytes, byte type) throws InvalidProtocolBufferException {
|
||||
|
||||
|
||||
|
||||
if(CIMConstant.ProtobufType.S_H_RQ == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.S_H_RQ == type) {
|
||||
HeartbeatRequest request = HeartbeatRequest.getInstance();
|
||||
logger.info(request.toString());
|
||||
return request;
|
||||
}
|
||||
|
||||
if(CIMConstant.ProtobufType.REPLYBODY == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.REPLYBODY == type) {
|
||||
ReplyBodyProto.Model bodyProto = ReplyBodyProto.Model.parseFrom(bytes);
|
||||
ReplyBody body = new ReplyBody();
|
||||
body.setKey(bodyProto.getKey());
|
||||
@ -105,8 +99,7 @@ public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
return body;
|
||||
}
|
||||
|
||||
if(CIMConstant.ProtobufType.MESSAGE == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.MESSAGE == type) {
|
||||
MessageProto.Model bodyProto = MessageProto.Model.parseFrom(bytes);
|
||||
Message message = new Message();
|
||||
message.setMid(bodyProto.getMid());
|
||||
@ -129,6 +122,7 @@ public class ClientMessageDecoder extends CumulativeProtocolDecoder {
|
||||
|
||||
/**
|
||||
* 解析消息体长度
|
||||
*
|
||||
* @param type
|
||||
* @param length
|
||||
* @return
|
||||
|
@ -29,12 +29,14 @@ import org.apache.mina.filter.codec.ProtocolEncoderOutput;
|
||||
|
||||
import com.farsunset.cim.sdk.client.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.client.model.Protobufable;
|
||||
|
||||
/**
|
||||
* 客户端消息发送前进行编码
|
||||
*/
|
||||
public class ClientMessageEncoder extends ProtocolEncoderAdapter {
|
||||
|
||||
protected final Logger logger = Logger.getLogger(ClientMessageEncoder.class);
|
||||
|
||||
@Override
|
||||
public void encode(IoSession iosession, Object object, ProtocolEncoderOutput out) throws Exception {
|
||||
|
||||
@ -58,6 +60,7 @@ public class ClientMessageEncoder extends ProtocolEncoderAdapter {
|
||||
|
||||
/**
|
||||
* 消息体最大为65535
|
||||
*
|
||||
* @param type
|
||||
* @param length
|
||||
* @return
|
||||
|
@ -54,7 +54,6 @@ public class HeartbeatRequest implements Serializable,Protobufable {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte getType() {
|
||||
return CIMConstant.ProtobufType.S_H_RQ;
|
||||
|
@ -43,6 +43,7 @@ public class HeartbeatResponse implements Serializable,Protobufable {
|
||||
public static HeartbeatResponse getInstance() {
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getByteArray() {
|
||||
return CMD_HEARTBEAT_RESPONSE.getBytes();
|
||||
|
@ -23,6 +23,7 @@ package com.farsunset.cim.sdk.client.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* java |android 客户端请求结构
|
||||
*
|
||||
@ -35,7 +36,6 @@ public class Intent implements Serializable {
|
||||
|
||||
private HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
|
||||
|
||||
public Intent() {
|
||||
}
|
||||
|
||||
@ -54,6 +54,7 @@ public class Intent implements Serializable {
|
||||
public void putExtra(String key, Object value) {
|
||||
data.put(key, value);
|
||||
}
|
||||
|
||||
public Object getExtra(String key) {
|
||||
return data.get(key);
|
||||
}
|
||||
|
@ -30,13 +30,11 @@ public class Message implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
private String mid;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
@ -59,8 +57,6 @@ public class Message implements Serializable {
|
||||
*/
|
||||
private String receiver;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* content 内容格式
|
||||
*/
|
||||
@ -73,11 +69,10 @@ public class Message implements Serializable {
|
||||
|
||||
private long timestamp;
|
||||
|
||||
|
||||
public Message()
|
||||
{
|
||||
public Message() {
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -86,14 +81,14 @@ public class Message implements Serializable {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@ -126,7 +121,6 @@ public class Message implements Serializable {
|
||||
this.receiver = receiver;
|
||||
}
|
||||
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
@ -135,14 +129,14 @@ public class Message implements Serializable {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getExtra() {
|
||||
return extra;
|
||||
}
|
||||
|
||||
public void setExtra(String extra) {
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
@ -159,7 +153,6 @@ public class Message implements Serializable {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
public String getMid() {
|
||||
return mid;
|
||||
}
|
||||
@ -172,5 +165,4 @@ public class Message implements Serializable {
|
||||
return txt != null && txt.trim().length() != 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.client.model;
|
||||
|
||||
/**
|
||||
* 需要向另一端发送的结构体
|
||||
*/
|
||||
|
@ -25,6 +25,7 @@ import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 请求应答对象
|
||||
*
|
||||
@ -38,7 +39,6 @@ public class ReplyBody implements Serializable {
|
||||
*/
|
||||
private String key;
|
||||
|
||||
|
||||
/**
|
||||
* 返回码
|
||||
*/
|
||||
@ -54,14 +54,13 @@ public class ReplyBody implements Serializable {
|
||||
*/
|
||||
private HashMap<String, String> data;
|
||||
|
||||
|
||||
private long timestamp;
|
||||
|
||||
public ReplyBody()
|
||||
{
|
||||
public ReplyBody() {
|
||||
data = new HashMap<String, String>();
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -70,8 +69,6 @@ public class ReplyBody implements Serializable {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
@ -118,9 +115,7 @@ public class ReplyBody implements Serializable {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#ReplyBody#").append("\n");
|
||||
buffer.append("key:").append(this.getKey()).append("\n");
|
||||
@ -129,8 +124,7 @@ public class ReplyBody implements Serializable {
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
|
@ -28,6 +28,7 @@ import java.util.Set;
|
||||
|
||||
import com.farsunset.cim.sdk.client.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.model.proto.SentBodyProto;
|
||||
|
||||
/**
|
||||
* java |android 客户端请求结构
|
||||
*
|
||||
@ -71,6 +72,7 @@ public class SentBody implements Serializable,Protobufable {
|
||||
data.put(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
public void putAll(Map<String, String> map) {
|
||||
data.putAll(map);
|
||||
}
|
||||
@ -79,7 +81,6 @@ public class SentBody implements Serializable,Protobufable {
|
||||
return data.keySet();
|
||||
}
|
||||
|
||||
|
||||
public void remove(String k) {
|
||||
data.remove(k);
|
||||
}
|
||||
@ -87,13 +88,13 @@ public class SentBody implements Serializable,Protobufable {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#SentBody#").append("\n");;
|
||||
buffer.append("#SentBody#").append("\n");
|
||||
;
|
||||
buffer.append("key:").append(key).append("\n");
|
||||
buffer.append("timestamp:").append(timestamp).append("\n");
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -27,6 +27,7 @@ package com.farsunset.cim.sdk.server.constant;
|
||||
public interface CIMConstant {
|
||||
// 消息头长度为3个字节,第一个字节为消息类型,第二,第三字节 转换int后为消息长度
|
||||
int DATA_HEADER_LENGTH = 3;
|
||||
|
||||
public static interface ReturnCode {
|
||||
|
||||
String CODE_200 = "200";
|
||||
@ -39,12 +40,9 @@ public interface CIMConstant {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
String SESSION_KEY = "account";
|
||||
String HEARTBEAT_KEY = "heartbeat";
|
||||
|
||||
|
||||
public static interface ProtobufType {
|
||||
byte S_H_RQ = 1;
|
||||
byte C_H_RS = 0;
|
||||
|
@ -25,12 +25,12 @@ import org.apache.mina.filter.codec.demux.DemuxingProtocolDecoder;
|
||||
|
||||
import com.farsunset.cim.sdk.server.filter.decoder.AppMessageDecoder;
|
||||
import com.farsunset.cim.sdk.server.filter.decoder.WebMessageDecoder;
|
||||
|
||||
/**
|
||||
* 服务端接收消息解码
|
||||
*/
|
||||
public class ServerMessageDecoder extends DemuxingProtocolDecoder {
|
||||
|
||||
|
||||
public ServerMessageDecoder() {
|
||||
addMessageDecoder(new AppMessageDecoder());
|
||||
addMessageDecoder(new WebMessageDecoder());
|
||||
|
@ -105,8 +105,8 @@ public class ServerMessageEncoder extends ProtocolEncoderAdapter {
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送到websocket的数据需要进行相关格式转换
|
||||
* 对传入数据进行无掩码转换
|
||||
* 发送到websocket的数据需要进行相关格式转换 对传入数据进行无掩码转换
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.server.filter.decoder;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.mina.core.buffer.IoBuffer;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
@ -31,6 +32,7 @@ import com.farsunset.cim.sdk.server.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.server.model.HeartbeatResponse;
|
||||
import com.farsunset.cim.sdk.server.model.SentBody;
|
||||
import com.farsunset.cim.sdk.server.model.proto.SentBodyProto;
|
||||
|
||||
/**
|
||||
* 服务端接收消息解码
|
||||
*/
|
||||
@ -38,7 +40,6 @@ public class AppMessageDecoder extends MessageDecoderAdapter {
|
||||
|
||||
protected final Logger logger = Logger.getLogger(AppMessageDecoder.class);
|
||||
|
||||
|
||||
@Override
|
||||
public MessageDecoderResult decodable(IoSession arg0, IoBuffer iobuffer) {
|
||||
|
||||
@ -57,10 +58,9 @@ public class AppMessageDecoder extends MessageDecoderAdapter {
|
||||
return NOT_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public MessageDecoderResult decode(IoSession iosession, IoBuffer iobuffer, ProtocolDecoderOutput out) throws Exception {
|
||||
public MessageDecoderResult decode(IoSession iosession, IoBuffer iobuffer, ProtocolDecoderOutput out)
|
||||
throws Exception {
|
||||
iobuffer.mark();
|
||||
|
||||
byte conetnType = iobuffer.get();
|
||||
@ -85,18 +85,15 @@ public class AppMessageDecoder extends MessageDecoderAdapter {
|
||||
return OK;
|
||||
}
|
||||
|
||||
public Object mappingMessageObject(byte[] data,byte type) throws Exception
|
||||
{
|
||||
public Object mappingMessageObject(byte[] data, byte type) throws Exception {
|
||||
|
||||
if(CIMConstant.ProtobufType.C_H_RS == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.C_H_RS == type) {
|
||||
HeartbeatResponse response = HeartbeatResponse.getInstance();
|
||||
logger.info(response.toString());
|
||||
return response;
|
||||
}
|
||||
|
||||
if(CIMConstant.ProtobufType.SENTBODY == type)
|
||||
{
|
||||
if (CIMConstant.ProtobufType.SENTBODY == type) {
|
||||
SentBodyProto.Model bodyProto = SentBodyProto.Model.parseFrom(data);
|
||||
SentBody body = new SentBody();
|
||||
body.setKey(bodyProto.getKey());
|
||||
@ -111,6 +108,7 @@ public class AppMessageDecoder extends MessageDecoderAdapter {
|
||||
|
||||
/**
|
||||
* 解析消息体长度
|
||||
*
|
||||
* @param type
|
||||
* @param length
|
||||
* @return
|
||||
@ -121,6 +119,4 @@ public class AppMessageDecoder extends MessageDecoderAdapter {
|
||||
return (l | (h <<= 8));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -45,7 +45,8 @@ public class WebMessageDecoder extends MessageDecoderAdapter {
|
||||
public static final byte HAS_EXTEND_DATA = 126;
|
||||
public static final byte HAS_EXTEND_DATA_CONTINUE = 127;
|
||||
public static final byte PAYLOADLEN = 0x7F;// 0111 1111
|
||||
public static final Pattern SEC_KEY_PATTERN = Pattern.compile("^(Sec-WebSocket-Key:).+",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
|
||||
public static final Pattern SEC_KEY_PATTERN = Pattern.compile("^(Sec-WebSocket-Key:).+",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
|
||||
|
||||
protected final Logger logger = Logger.getLogger(WebMessageDecoder.class);
|
||||
|
||||
@ -57,8 +58,7 @@ public class WebMessageDecoder extends MessageDecoderAdapter {
|
||||
|
||||
/**
|
||||
* 原生SDK只会发送2种类型消息 1个心跳类型 另一个是sendbody,报文的第一个字节为消息类型
|
||||
* 如果非原生sdk发出的消息,则认为是websocket发送的消息
|
||||
* websocket发送的消息 第一个字节不可能等于C_H_RS或者SENTBODY
|
||||
* 如果非原生sdk发出的消息,则认为是websocket发送的消息 websocket发送的消息 第一个字节不可能等于C_H_RS或者SENTBODY
|
||||
*/
|
||||
byte conetnType = iobuffer.get();
|
||||
if (conetnType == CIMConstant.ProtobufType.C_H_RS || conetnType == CIMConstant.ProtobufType.SENTBODY) {
|
||||
@ -149,7 +149,6 @@ public class WebMessageDecoder extends MessageDecoderAdapter {
|
||||
out.write(body);
|
||||
}
|
||||
|
||||
|
||||
public void handleSentBodyAndHeartPing(byte[] data, ProtocolDecoderOutput out) throws UnsupportedEncodingException {
|
||||
String message = new String(data, "UTF-8");
|
||||
|
||||
@ -160,8 +159,7 @@ public class WebMessageDecoder extends MessageDecoderAdapter {
|
||||
HeartbeatResponse response = HeartbeatResponse.getInstance();
|
||||
logger.info(response.toString());
|
||||
out.write(response);
|
||||
}else if(data.length > 2)
|
||||
{
|
||||
} else if (data.length > 2) {
|
||||
SentBody body = JSON.parseObject(message, SentBody.class);
|
||||
logger.info(body.toString());
|
||||
out.write(body);
|
||||
|
@ -59,8 +59,7 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
private final int TIME_OUT = 10;// 秒
|
||||
private final int READ_BUFFER_SIZE = 1024;// byte
|
||||
|
||||
public void bind() throws IOException
|
||||
{
|
||||
public void bind() throws IOException {
|
||||
|
||||
/**
|
||||
* 预制websocket握手请求的处理
|
||||
@ -72,7 +71,6 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
((DefaultSocketSessionConfig) acceptor.getSessionConfig()).setKeepAlive(true);
|
||||
((DefaultSocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true);
|
||||
|
||||
|
||||
KeepAliveFilter keepAliveFilter = new KeepAliveFilter(this, IdleStatus.WRITER_IDLE);
|
||||
keepAliveFilter.setRequestInterval(IDLE_TIME);
|
||||
keepAliveFilter.setRequestTimeout(TIME_OUT);
|
||||
@ -88,12 +86,10 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
acceptor.bind(new InetSocketAddress(port));
|
||||
}
|
||||
|
||||
public void unbind()
|
||||
{
|
||||
public void unbind() {
|
||||
acceptor.unbind();
|
||||
}
|
||||
|
||||
|
||||
public void sessionCreated(IoSession session) {
|
||||
logger.warn("sessionCreated()... from " + session.getRemoteAddress() + " nid:" + session.getId());
|
||||
}
|
||||
@ -102,7 +98,6 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
|
||||
SentBody body = (SentBody) message;
|
||||
|
||||
|
||||
CIMRequestHandler handler = handlers.get(body.getKey());
|
||||
if (handler == null) {
|
||||
|
||||
@ -114,14 +109,12 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
|
||||
} else {
|
||||
ReplyBody reply = handler.process(new CIMSession(ios), body);
|
||||
if(reply!=null)
|
||||
{
|
||||
if (reply != null) {
|
||||
reply.setKey(body.getKey());
|
||||
ios.write(reply);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -130,10 +123,10 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
|
||||
CIMSession cimSession = new CIMSession(session);
|
||||
|
||||
logger.warn("sessionClosed()... from "+session.getRemoteAddress()+" nid:"+cimSession.getNid() +",isConnected:"+session.isConnected());
|
||||
logger.warn("sessionClosed()... from " + session.getRemoteAddress() + " nid:" + cimSession.getNid()
|
||||
+ ",isConnected:" + session.isConnected());
|
||||
CIMRequestHandler handler = handlers.get(CIMSESSION_CLOSED_HANDLER_KEY);
|
||||
if(handler!=null)
|
||||
{
|
||||
if (handler != null) {
|
||||
handler.process(cimSession, null);
|
||||
}
|
||||
}
|
||||
@ -148,7 +141,8 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
*/
|
||||
public void exceptionCaught(IoSession session, Throwable cause) {
|
||||
|
||||
logger.error("exceptionCaught()... from "+session.getRemoteAddress()+" isConnected:"+session.isConnected()+" nid:" + session.getId(),cause);
|
||||
logger.error("exceptionCaught()... from " + session.getRemoteAddress() + " isConnected:" + session.isConnected()
|
||||
+ " nid:" + session.getId(), cause);
|
||||
session.closeNow();
|
||||
}
|
||||
|
||||
@ -157,8 +151,6 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
public void messageSent(IoSession session, Object message) throws Exception {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Object getRequest(IoSession session) {
|
||||
return HeartbeatRequest.getInstance();
|
||||
@ -179,15 +171,12 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
|
||||
return arg1 instanceof HeartbeatResponse;
|
||||
}
|
||||
|
||||
public Map<Long,IoSession> getManagedSessions()
|
||||
{
|
||||
public Map<Long, IoSession> getManagedSessions() {
|
||||
return acceptor.getManagedSessions();
|
||||
}
|
||||
|
||||
public IoSession getManagedSession(Long nid)
|
||||
{
|
||||
if(nid == null)
|
||||
{
|
||||
public IoSession getManagedSession(Long nid) {
|
||||
if (nid == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -27,8 +27,10 @@ import com.farsunset.cim.sdk.server.model.ReplyBody;
|
||||
import com.farsunset.cim.sdk.server.model.SentBody;
|
||||
import com.farsunset.cim.sdk.server.model.WebsocketResponse;
|
||||
import com.farsunset.cim.sdk.server.session.CIMSession;
|
||||
|
||||
/**
|
||||
* 处理websocket握手请求,返回响应的报文给浏览器
|
||||
*
|
||||
* @author Iraid
|
||||
*
|
||||
*/
|
||||
|
@ -26,6 +26,7 @@ import java.io.UnsupportedEncodingException;
|
||||
|
||||
import com.farsunset.cim.sdk.server.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.server.model.feature.EncodeFormatable;
|
||||
|
||||
/**
|
||||
* 服务端心跳请求
|
||||
*
|
||||
@ -65,7 +66,6 @@ public class HeartbeatRequest implements Serializable,EncodeFormatable {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return CIMConstant.ProtobufType.S_H_RQ;
|
||||
|
@ -23,7 +23,6 @@ package com.farsunset.cim.sdk.server.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端心跳响应
|
||||
*/
|
||||
@ -42,10 +41,8 @@ public class HeartbeatResponse implements Serializable {
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.farsunset.cim.sdk.server.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.server.model.feature.EncodeFormatable;
|
||||
import com.farsunset.cim.sdk.server.model.proto.MessageProto;
|
||||
|
||||
/**
|
||||
* 消息对象
|
||||
*/
|
||||
@ -34,13 +35,11 @@ public class Message implements Serializable,EncodeFormatable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
private String mid;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
@ -75,12 +74,10 @@ public class Message implements Serializable,EncodeFormatable {
|
||||
|
||||
private long timestamp;
|
||||
|
||||
|
||||
|
||||
public Message()
|
||||
{
|
||||
public Message() {
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -89,14 +86,14 @@ public class Message implements Serializable,EncodeFormatable {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@ -129,7 +126,6 @@ public class Message implements Serializable,EncodeFormatable {
|
||||
this.receiver = receiver;
|
||||
}
|
||||
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
@ -138,17 +134,14 @@ public class Message implements Serializable,EncodeFormatable {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getExtra() {
|
||||
return extra;
|
||||
}
|
||||
|
||||
public void setExtra(String extra) {
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getMid() {
|
||||
return mid;
|
||||
}
|
||||
@ -176,6 +169,7 @@ public class Message implements Serializable,EncodeFormatable {
|
||||
public boolean isNotEmpty(String txt) {
|
||||
return txt != null && txt.trim().length() != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getProtobufBody() {
|
||||
MessageProto.Model.Builder builder = MessageProto.Model.newBuilder();
|
||||
@ -224,6 +218,7 @@ public class Message implements Serializable,EncodeFormatable {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return CIMConstant.ProtobufType.MESSAGE;
|
||||
|
@ -31,6 +31,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.farsunset.cim.sdk.server.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.server.model.feature.EncodeFormatable;
|
||||
import com.farsunset.cim.sdk.server.model.proto.ReplyBodyProto;
|
||||
|
||||
/**
|
||||
* 请求应答对象
|
||||
*
|
||||
@ -44,7 +45,6 @@ public class ReplyBody implements Serializable ,EncodeFormatable{
|
||||
*/
|
||||
private String key;
|
||||
|
||||
|
||||
/**
|
||||
* 返回码
|
||||
*/
|
||||
@ -60,13 +60,12 @@ public class ReplyBody implements Serializable ,EncodeFormatable{
|
||||
*/
|
||||
private HashMap<String, String> data = new HashMap<String, String>();
|
||||
|
||||
|
||||
private long timestamp;
|
||||
|
||||
public ReplyBody()
|
||||
{
|
||||
public ReplyBody() {
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -75,8 +74,6 @@ public class ReplyBody implements Serializable ,EncodeFormatable{
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
@ -124,8 +121,7 @@ public class ReplyBody implements Serializable ,EncodeFormatable{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#ReplyBody#").append("\n");
|
||||
buffer.append("key:").append(this.getKey()).append("\n");
|
||||
@ -134,8 +130,7 @@ public class ReplyBody implements Serializable ,EncodeFormatable{
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
@ -143,6 +138,7 @@ public class ReplyBody implements Serializable ,EncodeFormatable{
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getProtobufBody() {
|
||||
ReplyBodyProto.Model.Builder builder = ReplyBodyProto.Model.newBuilder();
|
||||
@ -163,6 +159,7 @@ public class ReplyBody implements Serializable ,EncodeFormatable{
|
||||
public byte getDataType() {
|
||||
return CIMConstant.ProtobufType.REPLYBODY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getJSONBody() {
|
||||
JSONObject json = new JSONObject();
|
||||
|
@ -25,6 +25,7 @@ import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* java |android 客户端请求结构
|
||||
*
|
||||
@ -59,7 +60,6 @@ public class SentBody implements Serializable {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
|
||||
public void remove(String k) {
|
||||
data.remove(k);
|
||||
}
|
||||
@ -78,18 +78,17 @@ public class SentBody implements Serializable {
|
||||
return data.keySet();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#SentBody#").append("\n");;
|
||||
buffer.append("#SentBody#").append("\n");
|
||||
;
|
||||
buffer.append("key:").append(key).append("\n");
|
||||
buffer.append("timestamp:").append(timestamp).append("\n");
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
|
@ -22,6 +22,7 @@
|
||||
package com.farsunset.cim.sdk.server.model;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* websocket握手响应结果
|
||||
*
|
||||
@ -42,6 +43,7 @@ public class WebsocketResponse{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
@ -20,11 +20,14 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.server.model.feature;
|
||||
|
||||
/**
|
||||
* 需要向另一端发送的结构体
|
||||
*/
|
||||
public interface EncodeFormatable {
|
||||
byte[] getProtobufBody();
|
||||
|
||||
byte[] getJSONBody();
|
||||
|
||||
byte getDataType();
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -73,19 +73,16 @@ public class CIMSession implements Serializable{
|
||||
private String location;// 位置
|
||||
private int apnsAble;// apns推送状态
|
||||
private int status;// 状态
|
||||
|
||||
public CIMSession(IoSession session) {
|
||||
this.session = session;
|
||||
this.nid = session.getId();
|
||||
}
|
||||
|
||||
public CIMSession()
|
||||
{
|
||||
public CIMSession() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getAccount() {
|
||||
return account;
|
||||
}
|
||||
@ -96,11 +93,6 @@ public class CIMSession implements Serializable{
|
||||
setAttribute(CIMConstant.SESSION_KEY, account);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
@ -151,7 +143,6 @@ public class CIMSession implements Serializable{
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
@ -178,15 +169,10 @@ public class CIMSession implements Serializable{
|
||||
setAttribute("deviceId", deviceId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Long getBindTime() {
|
||||
return bindTime;
|
||||
}
|
||||
@ -196,7 +182,6 @@ public class CIMSession implements Serializable{
|
||||
setAttribute("bindTime", bindTime);
|
||||
}
|
||||
|
||||
|
||||
public String getClientVersion() {
|
||||
return clientVersion;
|
||||
}
|
||||
@ -206,10 +191,6 @@ public class CIMSession implements Serializable{
|
||||
setAttribute("clientVersion", clientVersion);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String getSystemVersion() {
|
||||
return systemVersion;
|
||||
}
|
||||
@ -234,9 +215,6 @@ public class CIMSession implements Serializable{
|
||||
setAttribute("host", host);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public int getApnsAble() {
|
||||
return apnsAble;
|
||||
}
|
||||
@ -255,13 +233,11 @@ public class CIMSession implements Serializable{
|
||||
setAttribute("status", status);
|
||||
}
|
||||
|
||||
|
||||
public void setAttribute(String key, Object value) {
|
||||
if (session != null)
|
||||
session.setAttribute(key, value);
|
||||
}
|
||||
|
||||
|
||||
public boolean containsAttribute(String key) {
|
||||
if (session != null)
|
||||
return session.containsAttribute(key);
|
||||
@ -286,8 +262,7 @@ public class CIMSession implements Serializable{
|
||||
}
|
||||
|
||||
public boolean write(Object msg) {
|
||||
if(session!=null)
|
||||
{
|
||||
if (session != null) {
|
||||
WriteFuture future = session.write(msg);
|
||||
future.awaitUninterruptibly(10 * 1000);
|
||||
return future.isWritten();
|
||||
@ -297,21 +272,18 @@ public class CIMSession implements Serializable{
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
if(session != null)
|
||||
{
|
||||
if (session != null) {
|
||||
return session.isConnected();
|
||||
}
|
||||
|
||||
if(!isLocalhost())
|
||||
{
|
||||
if (!isLocalhost()) {
|
||||
return status == STATUS_ENABLED;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isLocalhost()
|
||||
{
|
||||
public boolean isLocalhost() {
|
||||
|
||||
try {
|
||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
||||
@ -323,7 +295,6 @@ public class CIMSession implements Serializable{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void closeNow() {
|
||||
if (session != null)
|
||||
session.closeNow();
|
||||
@ -334,7 +305,6 @@ public class CIMSession implements Serializable{
|
||||
session.closeOnFlush();
|
||||
}
|
||||
|
||||
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
setAttribute("packageName", apnsAble);
|
||||
@ -344,7 +314,6 @@ public class CIMSession implements Serializable{
|
||||
return packageName;
|
||||
}
|
||||
|
||||
|
||||
public int hashCode() {
|
||||
|
||||
return (deviceId + nid + host).hashCode();
|
||||
@ -363,8 +332,7 @@ public class CIMSession implements Serializable{
|
||||
if (o instanceof CIMSession) {
|
||||
|
||||
CIMSession t = (CIMSession) o;
|
||||
if(t.deviceId!=null && deviceId!=null)
|
||||
{
|
||||
if (t.deviceId != null && deviceId != null) {
|
||||
return !t.deviceId.equals(deviceId);
|
||||
}
|
||||
}
|
||||
@ -384,10 +352,7 @@ public class CIMSession implements Serializable{
|
||||
return session;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("{");
|
||||
|
||||
@ -397,7 +362,8 @@ public class CIMSession implements Serializable{
|
||||
buffer.append("\"").append("host").append("\":").append("\"").append(host).append("\"").append(",");
|
||||
buffer.append("\"").append("account").append("\":").append("\"").append(account).append("\"").append(",");
|
||||
buffer.append("\"").append("channel").append("\":").append("\"").append(channel).append("\"").append(",");
|
||||
buffer.append("\"").append("deviceModel").append("\":").append("\"").append(deviceModel).append("\"").append(",");
|
||||
buffer.append("\"").append("deviceModel").append("\":").append("\"").append(deviceModel).append("\"")
|
||||
.append(",");
|
||||
buffer.append("\"").append("status").append("\":").append(status).append(",");
|
||||
buffer.append("\"").append("apnsAble").append("\":").append(apnsAble).append(",");
|
||||
buffer.append("\"").append("bindTime").append("\":").append(bindTime).append(",");
|
||||
@ -407,6 +373,4 @@ public class CIMSession implements Serializable{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.server.session;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -28,25 +29,20 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import com.farsunset.cim.sdk.server.constant.CIMConstant;
|
||||
|
||||
/**
|
||||
* 自带默认 session管理实现, 各位可以自行实现 AbstractSessionManager接口来实现自己的 session管理
|
||||
*服务器集群时 须要将CIMSession 信息存入数据库或者nosql 等 第三方存储空间中,便于所有服务器都可以访问
|
||||
* 自带默认 session管理实现, 各位可以自行实现 AbstractSessionManager接口来实现自己的 session管理 服务器集群时
|
||||
* 须要将CIMSession 信息存入数据库或者nosql 等 第三方存储空间中,便于所有服务器都可以访问
|
||||
*/
|
||||
public class DefaultSessionManager implements SessionManager {
|
||||
|
||||
|
||||
private static HashMap<String, CIMSession> sessions = new HashMap<String, CIMSession>();
|
||||
|
||||
|
||||
private static final AtomicInteger connectionsCounter = new AtomicInteger(0);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void add(CIMSession session) {
|
||||
if(session!=null)
|
||||
{
|
||||
if (session != null) {
|
||||
session.setAttribute(CIMConstant.SESSION_KEY, session.getAccount());
|
||||
sessions.put(session.getAccount(), session);
|
||||
connectionsCounter.incrementAndGet();
|
||||
@ -54,14 +50,11 @@ public class DefaultSessionManager implements SessionManager{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public CIMSession get(String account) {
|
||||
|
||||
|
||||
return sessions.get(account);
|
||||
}
|
||||
|
||||
|
||||
public List<CIMSession> queryAll() {
|
||||
List<CIMSession> list = new ArrayList<CIMSession>();
|
||||
list.addAll(sessions.values());
|
||||
@ -70,24 +63,19 @@ public class DefaultSessionManager implements SessionManager{
|
||||
|
||||
public void remove(CIMSession session) {
|
||||
|
||||
|
||||
sessions.remove(session.getAttribute(CIMConstant.SESSION_KEY));
|
||||
}
|
||||
|
||||
|
||||
public void remove(String account) {
|
||||
|
||||
sessions.remove(account);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean containsCIMSession(String account)
|
||||
{
|
||||
public boolean containsCIMSession(String account) {
|
||||
return sessions.containsKey(account);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(CIMSession session) {
|
||||
sessions.put(session.getAccount(), session);
|
||||
|
@ -23,15 +23,12 @@ package com.farsunset.cim.sdk.server.session;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端的 session管理接口
|
||||
* 可自行实现此接口管理session
|
||||
* 客户端的 session管理接口 可自行实现此接口管理session
|
||||
*/
|
||||
|
||||
public interface SessionManager {
|
||||
|
||||
|
||||
/**
|
||||
* 添加新的session
|
||||
*/
|
||||
@ -44,20 +41,22 @@ public interface SessionManager {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param account 客户端session的 key 一般可用 用户账号来对应session
|
||||
* @param account
|
||||
* 客户端session的 key 一般可用 用户账号来对应session
|
||||
* @return
|
||||
*/
|
||||
CIMSession get(String account);
|
||||
|
||||
/**
|
||||
* 获取所有session
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<CIMSession> queryAll();
|
||||
|
||||
|
||||
/**
|
||||
* 删除session
|
||||
*
|
||||
* @param session
|
||||
*/
|
||||
public void remove(String account);
|
||||
|
@ -5,6 +5,8 @@ var SDK_VERSION = "1.0.0";
|
||||
var SDK_CHANNEL = "browser";
|
||||
var APP_PACKAGE = "com.farsunset.webcim";
|
||||
var ACCOUNT;
|
||||
const ACTION_999 = "999";//特殊的消息类型,代表被服务端强制下线
|
||||
|
||||
var socket;
|
||||
var manualStop = false;
|
||||
var CIMWebBridge = new Object();
|
||||
@ -37,6 +39,12 @@ CIMWebBridge.stop = function(){
|
||||
socket.close();
|
||||
};
|
||||
|
||||
CIMWebBridge.resume = function(){
|
||||
manualStop = false;
|
||||
CIMWebBridge.connection();
|
||||
};
|
||||
|
||||
|
||||
CIMWebBridge.innerOnConnectionSuccessed = function(){
|
||||
if(typeof onConnectionSuccessed != 'undefined' && onConnectionSuccessed instanceof Function){
|
||||
onConnectionSuccessed();
|
||||
@ -53,12 +61,13 @@ CIMWebBridge.innerOnMessageReceived = function(e){
|
||||
*/
|
||||
if(data == CMD_HEARTBEAT_REQUEST){
|
||||
socket.send(CMD_HEARTBEAT_RESPONSE);
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
var json = JSON.parse(data);
|
||||
if(json.contentType == "Message" && typeof onMessageReceived != 'undefined' && onMessageReceived instanceof Function){
|
||||
onMessageReceived(json);
|
||||
if(json.contentType == "Message"){
|
||||
onInterceptMessageReceived(json);
|
||||
return;
|
||||
}
|
||||
|
||||
if(json.contentType == "ReplyBody" && typeof onReplyReceived != 'undefined' && onReplyReceived instanceof Function){
|
||||
@ -76,6 +85,17 @@ CIMWebBridge.sendRequest = function(body){
|
||||
socket.send(json);
|
||||
};
|
||||
|
||||
function onInterceptMessageReceived(message){
|
||||
//被强制下线之后,不再继续连接服务端
|
||||
if(message.action == ACTION_999){
|
||||
manualStop = true;
|
||||
}
|
||||
//收到消息后,将消息发送给页面
|
||||
if(onMessageReceived instanceof Function){
|
||||
onMessageReceived(message);
|
||||
}
|
||||
}
|
||||
|
||||
function getBrowser() {
|
||||
var explorer = window.navigator.userAgent.toLowerCase() ;
|
||||
// ie
|
||||
|
@ -49,6 +49,11 @@
|
||||
|
||||
function onMessageReceived(message)
|
||||
{
|
||||
if(message.acction == ACTION_999){
|
||||
doHideDialog('MessageDialog');
|
||||
doShowDialog('LoginDialog');
|
||||
return ;
|
||||
}
|
||||
$("#messageList").append("<div class='alert alert-info' >"+message.content+"</div>");
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.admin.action;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
@ -32,6 +31,7 @@ import com.farsunset.cim.sdk.server.model.Message;
|
||||
import com.farsunset.cim.sdk.server.session.DefaultSessionManager;
|
||||
import com.farsunset.cim.util.ContextHolder;
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
|
||||
public class SessionAction extends ActionSupport {
|
||||
|
||||
/**
|
||||
@ -39,20 +39,15 @@ public class SessionAction extends ActionSupport {
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public String list() {
|
||||
|
||||
|
||||
|
||||
|
||||
public String list()
|
||||
{
|
||||
|
||||
ServletActionContext.getRequest().setAttribute("sessionList", ((DefaultSessionManager) ContextHolder.getBean("CIMSessionManager")).queryAll());
|
||||
ServletActionContext.getRequest().setAttribute("sessionList",
|
||||
((DefaultSessionManager) ContextHolder.getBean("CIMSessionManager")).queryAll());
|
||||
|
||||
return "list";
|
||||
}
|
||||
|
||||
public void offline() throws IOException
|
||||
{
|
||||
public void offline() throws IOException {
|
||||
|
||||
String account = ServletActionContext.getRequest().getParameter("account");
|
||||
Message msg = new Message();
|
||||
|
@ -51,7 +51,8 @@ public class MessageAction extends ActionSupport implements ModelDriven<Messa
|
||||
|
||||
/**
|
||||
* 关于http参数获取, struts2 的模型驱动 比如 http 参数 sender=xiaomao&receiver=xiaogou
|
||||
* struts自动会将参数的值 存入getModel()返回的对象的对应属性中,即xiaomao会存入message.sender属性,xiaogou会存入message.receiver属性
|
||||
* struts自动会将参数的值
|
||||
* 存入getModel()返回的对象的对应属性中,即xiaomao会存入message.sender属性,xiaogou会存入message.receiver属性
|
||||
* 省去了request.getParameter("sender")方式获取参数,,如果参数名在getModel()返回的对象中不存在,则需要用request.getParameter()获取
|
||||
* 其他相关*Action.java中 同理,这里做统一说明!
|
||||
*/
|
||||
@ -66,12 +67,10 @@ public class MessageAction extends ActionSupport implements ModelDriven<Messa
|
||||
|
||||
checkParams();
|
||||
message.setMid(StringUtil.getUUID());
|
||||
if(Constants.MessageType.TYPE_2.equals(message.getAction()))
|
||||
{
|
||||
if (Constants.MessageType.TYPE_2.equals(message.getAction())) {
|
||||
// 向客户端 发送消息
|
||||
ContextHolder.getBean(SystemMessagePusher.class).push(message);
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
// 向客户端 发送消息
|
||||
((DefaultMessagePusher) ContextHolder.getBean("messagePusher")).push(message);
|
||||
}
|
||||
@ -89,45 +88,32 @@ public class MessageAction extends ActionSupport implements ModelDriven<Messa
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 文件由客户端发往阿里云 OSS 存储
|
||||
*
|
||||
* @param messageServiceImpl
|
||||
*/
|
||||
/* private void fileHandler(Message mo, HttpServletRequest request) throws IOException
|
||||
{
|
||||
if(request instanceof MultiPartRequestWrapper){
|
||||
MultiPartRequestWrapper pr = (MultiPartRequestWrapper) request;
|
||||
if(pr.getFiles("file")!=null)
|
||||
{
|
||||
File file = pr.getFiles("file")[0];
|
||||
/*
|
||||
* private void fileHandler(Message mo, HttpServletRequest request) throws
|
||||
* IOException { if(request instanceof MultiPartRequestWrapper){
|
||||
* MultiPartRequestWrapper pr = (MultiPartRequestWrapper) request;
|
||||
* if(pr.getFiles("file")!=null) { File file = pr.getFiles("file")[0];
|
||||
*
|
||||
* String fileType = request.getParameter("fileType"); String dir =
|
||||
* dirMap.get(fileType); if(StringUtils.isEmpty(dir)) { throw new
|
||||
* IllegalArgumentException("fileType:" +fileType+" 未定义" );
|
||||
*
|
||||
* } String path = request.getSession().getServletContext().getRealPath(dir);
|
||||
* String uuid=UUID.randomUUID().toString().replaceAll("-", ""); File des = new
|
||||
* File(path+"/"+uuid); FileUtil.copyFile(file, des); mo.setFile(dir+"/"+uuid);
|
||||
* mo.setFileType(fileType); } }
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
String fileType = request.getParameter("fileType");
|
||||
String dir = dirMap.get(fileType);
|
||||
if(StringUtils.isEmpty(dir))
|
||||
{
|
||||
throw new IllegalArgumentException("fileType:" +fileType+" 未定义" );
|
||||
private void checkParams() throws ServletRequestBindingException {
|
||||
|
||||
}
|
||||
String path = request.getSession().getServletContext().getRealPath(dir);
|
||||
String uuid=UUID.randomUUID().toString().replaceAll("-", "");
|
||||
File des = new File(path+"/"+uuid);
|
||||
FileUtil.copyFile(file, des);
|
||||
mo.setFile(dir+"/"+uuid);
|
||||
mo.setFileType(fileType);
|
||||
}
|
||||
}
|
||||
|
||||
}*/
|
||||
|
||||
|
||||
private void checkParams() throws ServletRequestBindingException
|
||||
{
|
||||
|
||||
if(StringUtils.isEmpty(message.getReceiver()))
|
||||
{
|
||||
if (StringUtils.isEmpty(message.getReceiver())) {
|
||||
throw new IllegalArgumentException("receiver 不能为空!");
|
||||
|
||||
}
|
||||
@ -139,6 +125,4 @@ public class MessageAction extends ActionSupport implements ModelDriven<Messa
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -35,7 +35,6 @@ import com.farsunset.cim.sdk.server.session.DefaultSessionManager;
|
||||
import com.farsunset.cim.util.ContextHolder;
|
||||
import com.farsunset.cim.util.StringUtil;
|
||||
|
||||
|
||||
/**
|
||||
* 账号绑定实现
|
||||
*
|
||||
@ -78,19 +77,15 @@ public class BindHandler implements CIMRequestHandler {
|
||||
|
||||
oldSession.setStatus(CIMSession.STATUS_ENABLED);
|
||||
sessionManager.update(oldSession);
|
||||
reply.put("sid", oldSession.getGid());
|
||||
return reply;
|
||||
}
|
||||
|
||||
|
||||
closeQuietly(oldSession);
|
||||
|
||||
|
||||
// 第一次设置心跳时间为登录时间
|
||||
newSession.setBindTime(System.currentTimeMillis());
|
||||
newSession.setHeartbeat(System.currentTimeMillis());
|
||||
|
||||
|
||||
sessionManager.add(newSession);
|
||||
|
||||
} catch (Exception e) {
|
||||
@ -98,12 +93,9 @@ public class BindHandler implements CIMRequestHandler {
|
||||
e.printStackTrace();
|
||||
}
|
||||
logger.debug("bind :account:" + message.get("account") + "-----------------------------" + reply.getCode());
|
||||
reply.put("sid", newSession.getGid());
|
||||
return reply;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void sendForceOfflineMessage(CIMSession oldSession, String account, String deviceModel) {
|
||||
|
||||
Message msg = new Message();
|
||||
@ -111,14 +103,10 @@ public class BindHandler implements CIMRequestHandler {
|
||||
msg.setAction(CIMConstant.MessageAction.ACTION_999);// 强行下线消息类型
|
||||
msg.setReceiver(account);
|
||||
msg.setContent(deviceModel);
|
||||
|
||||
closeQuietly(oldSession, msg);
|
||||
|
||||
if (!oldSession.isLocalhost()) {
|
||||
ContextHolder.getBean(SystemMessagePusher.class).push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 同一设备切换网络时关闭旧的连接
|
||||
private void closeQuietly(CIMSession oldSession) {
|
||||
|
@ -53,6 +53,4 @@ public class SessionClosedHandler implements CIMRequestHandler {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -23,20 +23,17 @@ package com.farsunset.cim.push;
|
||||
|
||||
import com.farsunset.cim.sdk.server.model.Message;
|
||||
|
||||
|
||||
/**
|
||||
* 消息发送实接口
|
||||
*
|
||||
*/
|
||||
public interface CIMMessagePusher {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 向用户发送消息
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public void push(Message msg);
|
||||
|
||||
|
||||
}
|
||||
|
@ -31,29 +31,26 @@ import com.farsunset.cim.sdk.server.session.DefaultSessionManager;
|
||||
*/
|
||||
public class DefaultMessagePusher implements CIMMessagePusher {
|
||||
|
||||
|
||||
private DefaultSessionManager sessionManager;
|
||||
|
||||
|
||||
public void setSessionManager(DefaultSessionManager sessionManager) {
|
||||
this.sessionManager = sessionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向用户发送消息
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public void push(Message msg) {
|
||||
CIMSession session = sessionManager.get(msg.getReceiver());
|
||||
|
||||
/*
|
||||
* 服务器集群时,可以在此
|
||||
* 判断当前session是否连接于本台服务器,如果是,继续往下走,如果不是,将此消息发往当前session连接的服务器并 return
|
||||
* if(session!=null&&!session.isLocalhost()){//判断当前session是否连接于本台服务器,如不是
|
||||
* 服务器集群时,可以在此 判断当前session是否连接于本台服务器,如果是,继续往下走,如果不是,将此消息发往当前session连接的服务器并
|
||||
* return if(session!=null&&!session.isLocalhost()){//判断当前session是否连接于本台服务器,如不是
|
||||
* //发往目标服务器处理
|
||||
* MessageDispatcher.execute(MessageUtil.transform(msg),session.getHost());
|
||||
* return;
|
||||
* }
|
||||
* return; }
|
||||
*/
|
||||
|
||||
if (session != null && session.isConnected()) {
|
||||
@ -72,25 +69,22 @@ public class DefaultMessagePusher implements CIMMessagePusher {
|
||||
|
||||
/**
|
||||
* 引入javaapns相关jar
|
||||
*
|
||||
* @param badge
|
||||
* @param content
|
||||
* @param token
|
||||
*/
|
||||
private void apnsPush(int badge, String content, String token) {
|
||||
/*String password = "password";
|
||||
String keystore = "p12 文件 绝对路径";
|
||||
boolean isDebug = true;
|
||||
|
||||
PushNotificationPayload payload = PushNotificationPayload.complex();
|
||||
try {
|
||||
payload.addAlert(content);
|
||||
payload.addBadge(1);
|
||||
payload.addSound("default");
|
||||
Push.payload(payload, keystore, password, isDebug, token);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}*/
|
||||
/*
|
||||
* String password = "password"; String keystore = "p12 文件 绝对路径"; boolean
|
||||
* isDebug = true;
|
||||
*
|
||||
* PushNotificationPayload payload = PushNotificationPayload.complex(); try {
|
||||
* payload.addAlert(content); payload.addBadge(1); payload.addSound("default");
|
||||
* Push.payload(payload, keystore, password, isDebug, token);
|
||||
*
|
||||
* } catch (Exception e) { e.printStackTrace(); }
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ import com.farsunset.cim.sdk.server.model.Message;
|
||||
|
||||
public class SystemMessagePusher extends DefaultMessagePusher {
|
||||
|
||||
|
||||
@Override
|
||||
public void push(Message message) {
|
||||
|
||||
|
@ -26,34 +26,30 @@ import com.farsunset.cim.sdk.server.session.CIMSession;
|
||||
import com.farsunset.cim.sdk.server.session.SessionManager;
|
||||
|
||||
/**
|
||||
* 集群 session管理实现示例, 各位可以自行实现 AbstractSessionManager接口来实现自己的 session管理
|
||||
*服务器集群时 须要将CIMSession 信息存入数据库或者nosql 等 第三方存储空间中,便于所有服务器都可以访问
|
||||
* 集群 session管理实现示例, 各位可以自行实现 AbstractSessionManager接口来实现自己的 session管理 服务器集群时
|
||||
* 须要将CIMSession 信息存入数据库或者nosql 等 第三方存储空间中,便于所有服务器都可以访问
|
||||
*/
|
||||
public class ClusterSessionManager implements SessionManager {
|
||||
|
||||
|
||||
|
||||
|
||||
public CIMSession get(String account) {
|
||||
|
||||
// 这里查询数据库
|
||||
/*CIMSession session = database.getSession(account);
|
||||
session.setIoSession(ContextHolder.getBean(CIMNioSocketAcceptor.class).getManagedSessions().get(session.getNid()));
|
||||
return session;*/
|
||||
/*
|
||||
* CIMSession session = database.getSession(account);
|
||||
* session.setIoSession(ContextHolder.getBean(CIMNioSocketAcceptor.class).
|
||||
* getManagedSessions().get(session.getNid())); return session;
|
||||
*/
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<CIMSession> queryAll() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void remove(String account) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,6 +62,4 @@ public class ClusterSessionManager implements SessionManager{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ package com.farsunset.cim.util;
|
||||
|
||||
public interface Constants {
|
||||
|
||||
|
||||
public class Common {
|
||||
public final static String STATUS_1 = "1";
|
||||
public final static String STATUS_0 = "0";
|
||||
@ -34,6 +33,7 @@ public interface Constants {
|
||||
public final static String TYPR_9 = "9";
|
||||
|
||||
}
|
||||
|
||||
public class User {
|
||||
public final static Integer SUCCESS_CODE = 1;
|
||||
public final static Integer ERROR_CODE = 0;
|
||||
@ -41,8 +41,7 @@ public interface Constants {
|
||||
|
||||
}
|
||||
|
||||
public class RequestParam
|
||||
{
|
||||
public class RequestParam {
|
||||
public final static String CURRENTPAGE = "currentPage";
|
||||
public final static String TYPE = "type";
|
||||
public static final String SIZE = "size";
|
||||
@ -59,7 +58,6 @@ public interface Constants {
|
||||
|
||||
public static interface MessageType {
|
||||
|
||||
|
||||
// 用户之间的普通消息
|
||||
public static final String TYPE_0 = "0";
|
||||
|
||||
|
@ -24,25 +24,22 @@ package com.farsunset.cim.util;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
public class ContextHolder implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext context;
|
||||
|
||||
public static Object getBean(String name)
|
||||
{
|
||||
public static Object getBean(String name) {
|
||||
return context.getBean(name);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> c)
|
||||
{
|
||||
public static <T> T getBean(Class<T> c) {
|
||||
|
||||
return context.getBean(c);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
context = applicationContext;
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@ -39,15 +40,12 @@ import org.apache.http.util.EntityUtils;
|
||||
|
||||
import com.farsunset.cim.sdk.server.model.Message;
|
||||
|
||||
|
||||
|
||||
public class MessageDispatcher {
|
||||
private static BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
|
||||
private static ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 20, TimeUnit.SECONDS, queue);;
|
||||
final static String sendUrl = "http://%1$s:8080/cim-servier/cgi/message_send.api";
|
||||
|
||||
public static void execute(final Message msg,final String ip)
|
||||
{
|
||||
public static void execute(final Message msg, final String ip) {
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -60,9 +58,7 @@ public class MessageDispatcher {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private static String httpPost(String url,Message msg) throws Exception
|
||||
{
|
||||
private static String httpPost(String url, Message msg) throws Exception {
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
||||
@ -85,10 +81,7 @@ public class MessageDispatcher {
|
||||
response2.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -25,35 +25,29 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class StringUtil {
|
||||
|
||||
|
||||
public static boolean isEmpty(Object obj)
|
||||
{
|
||||
public static boolean isEmpty(Object obj) {
|
||||
if (null == obj)
|
||||
return true;
|
||||
if("".equals(obj.toString().trim()))
|
||||
{
|
||||
if ("".equals(obj.toString().trim())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(Object obj)
|
||||
{
|
||||
public static boolean isNotEmpty(Object obj) {
|
||||
|
||||
return !isEmpty(obj);
|
||||
}
|
||||
public static String getSequenceId()
|
||||
{
|
||||
|
||||
public static String getSequenceId() {
|
||||
String mark = String.valueOf(System.currentTimeMillis());
|
||||
return mark;
|
||||
}
|
||||
|
||||
public static String getCurrentlyDateTime() {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(
|
||||
"yyyyMMddHHmmss");
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
return dateFormat.format(new Date());
|
||||
}
|
||||
|
||||
@ -73,5 +67,4 @@ public class StringUtil {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
class CIMCacheManager {
|
||||
|
||||
class CIMCacheManager {
|
||||
|
||||
public static final String CIM_CONFIG_INFO = "CIM_CONFIG_INFO";
|
||||
|
||||
@ -45,16 +45,12 @@ class CIMCacheManager {
|
||||
|
||||
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
|
||||
|
||||
|
||||
public static void remove(Context context ,String key)
|
||||
{
|
||||
public static void remove(Context context, String key) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
resolver.delete(Uri.parse(CIMCacheProvider.CONTENT_URI), key, null);
|
||||
}
|
||||
|
||||
|
||||
public static void putString(Context context ,String key,String value)
|
||||
{
|
||||
public static void putString(Context context, String key, String value) {
|
||||
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
ContentValues values = new ContentValues();
|
||||
@ -64,13 +60,11 @@ class CIMCacheManager {
|
||||
|
||||
}
|
||||
|
||||
public static String getString(Context context ,String key)
|
||||
{
|
||||
public static String getString(Context context, String key) {
|
||||
String value = null;
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
Cursor cursor = resolver.query(Uri.parse(CIMCacheProvider.CONTENT_URI), new String[] { key }, null, null, null);
|
||||
if (cursor!=null && cursor.moveToFirst())
|
||||
{
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
value = cursor.getString(0);
|
||||
cursor.close();
|
||||
}
|
||||
@ -82,28 +76,24 @@ class CIMCacheManager {
|
||||
try {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}catch(Exception e){}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void putBoolean(Context context,String key,boolean value)
|
||||
{
|
||||
public static void putBoolean(Context context, String key, boolean value) {
|
||||
putString(context, key, Boolean.toString(value));
|
||||
}
|
||||
|
||||
public static boolean getBoolean(Context context,String key)
|
||||
{
|
||||
public static boolean getBoolean(Context context, String key) {
|
||||
String value = getString(context, key);
|
||||
return value == null ? false : Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
|
||||
public static void putInt(Context context,String key,int value)
|
||||
{
|
||||
public static void putInt(Context context, String key, int value) {
|
||||
putString(context, key, String.valueOf(value));
|
||||
}
|
||||
|
||||
public static int getInt(Context context,String key)
|
||||
{
|
||||
public static int getInt(Context context, String key) {
|
||||
String value = getString(context, key);
|
||||
return value == null ? 0 : Integer.parseInt(value);
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ public class CIMCacheProvider extends ContentProvider {
|
||||
public static final String CONTENT_URI = "content://com.farsunset.cim.provider";
|
||||
static final String MODEL_KEY = "PRIVATE_CIM_CONFIG";
|
||||
|
||||
|
||||
@Override
|
||||
public int delete(Uri arg0, String key, String[] arg2) {
|
||||
getContext().getSharedPreferences(MODEL_KEY, Context.MODE_PRIVATE).edit().remove(key).apply();
|
||||
@ -70,5 +69,4 @@ public class CIMCacheProvider extends ContentProvider {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,170 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013-2023 Xia Jun(3979434@qq.com).
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
***************************************************************************************
|
||||
* *
|
||||
* Website : http://www.farsunset.com *
|
||||
* *
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
class CIMCacheToolkit extends SQLiteOpenHelper {
|
||||
|
||||
private static final String DATABASE_NAME = "CIM_CONFIG_INFO.db";
|
||||
private static final int DATABASE_VERSION = 20160406;
|
||||
private static final String TABLE_NAME = "T_CIM_CONFIG";
|
||||
private static CIMCacheToolkit toolkit;
|
||||
|
||||
private static final String TABLE_SQL = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" (KEY VARCHAR(64) PRIMARY KEY,VALUE TEXT)";
|
||||
|
||||
private static final String DELETE_SQL = "DELETE FROM "+TABLE_NAME+" WHERE KEY = ?";
|
||||
|
||||
private static final String QUERY_SQL = "SELECT VALUE FROM "+TABLE_NAME+" WHERE KEY = ?";
|
||||
|
||||
private SQLiteDatabase mSQLiteDatabase;
|
||||
|
||||
public static final String CIM_CONFIG_INFO = "CIM_CONFIG_INFO";
|
||||
|
||||
public static final String KEY_ACCOUNT = "KEY_ACCOUNT";
|
||||
|
||||
public static final String KEY_MANUAL_STOP = "KEY_MANUAL_STOP";
|
||||
|
||||
public static final String KEY_CIM_DESTROYED = "KEY_CIM_DESTROYED";
|
||||
|
||||
public static final String KEY_CIM_SERVIER_HOST = "KEY_CIM_SERVIER_HOST";
|
||||
|
||||
public static final String KEY_CIM_SERVIER_PORT = "KEY_CIM_SERVIER_PORT";
|
||||
|
||||
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
|
||||
|
||||
|
||||
public synchronized static CIMCacheToolkit getInstance(Context context){
|
||||
|
||||
if(toolkit==null){
|
||||
toolkit = new CIMCacheToolkit(context);
|
||||
}
|
||||
|
||||
return toolkit;
|
||||
}
|
||||
|
||||
|
||||
public CIMCacheToolkit(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
|
||||
super(context, name, factory, version);
|
||||
}
|
||||
|
||||
public CIMCacheToolkit(Context context){
|
||||
this(context, DATABASE_NAME,null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
|
||||
public synchronized void remove(String key)
|
||||
{
|
||||
getSQLiteDatabase().execSQL(DELETE_SQL,new String[]{key});
|
||||
}
|
||||
|
||||
|
||||
public synchronized void putString(String key,String value)
|
||||
{
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("VALUE", value);
|
||||
int result = getSQLiteDatabase().updateWithOnConflict(TABLE_NAME, values, "KEY=?",new String[]{key},SQLiteDatabase.CONFLICT_FAIL);
|
||||
if(result<=0){
|
||||
|
||||
values.put("KEY", key);
|
||||
getSQLiteDatabase().insert(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public synchronized String getString(String key)
|
||||
{
|
||||
String value = null;
|
||||
Cursor cursor = getSQLiteDatabase().rawQuery(QUERY_SQL, new String[]{key});
|
||||
if (cursor!=null)
|
||||
{
|
||||
if(cursor.moveToFirst()){
|
||||
value = cursor.getString(0);
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public void putBoolean(String key,boolean value)
|
||||
{
|
||||
putString(key,Boolean.toString(value));
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key)
|
||||
{
|
||||
String value = getString(key);
|
||||
return value == null?false:Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
|
||||
public void putInt(String key,int value)
|
||||
{
|
||||
putString(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
public int getInt(String key)
|
||||
{
|
||||
String value = getString(key);
|
||||
return value == null?0:Integer.parseInt(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase database) {
|
||||
database.execSQL(TABLE_SQL);
|
||||
}
|
||||
|
||||
|
||||
public static synchronized void destroy(){
|
||||
if (toolkit!=null){
|
||||
try{toolkit.mSQLiteDatabase.close();}catch(Exception e){}
|
||||
try{toolkit.close();}catch(Exception e){}
|
||||
}
|
||||
|
||||
toolkit = null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private SQLiteDatabase getSQLiteDatabase(){
|
||||
if(mSQLiteDatabase!=null){
|
||||
return mSQLiteDatabase;
|
||||
}else
|
||||
{
|
||||
mSQLiteDatabase = getWritableDatabase();
|
||||
}
|
||||
return mSQLiteDatabase;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
|
||||
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -79,8 +80,6 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
|
||||
private static CIMConnectorManager manager;
|
||||
|
||||
|
||||
|
||||
private CIMConnectorManager(Context ctx) {
|
||||
context = ctx;
|
||||
|
||||
@ -152,8 +151,7 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
}
|
||||
|
||||
executor.execute(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
public void run() {
|
||||
syncConnection(host, port);
|
||||
}
|
||||
});
|
||||
@ -166,8 +164,7 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
|
||||
String exceptionName = SessionClosedException.class.getSimpleName();
|
||||
|
||||
if(channel!=null && channel.isActive())
|
||||
{
|
||||
if (channel != null && channel.isActive()) {
|
||||
ChannelFuture future = channel.writeAndFlush(body);
|
||||
isSuccessed = future.awaitUninterruptibly(WRITE_TIMEOUT);
|
||||
if (!isSuccessed && future.cause() != null) {
|
||||
@ -182,8 +179,7 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
intent.putExtra(Exception.class.getName(), exceptionName);
|
||||
intent.putExtra(SentBody.class.getName(), body);
|
||||
context.sendBroadcast(intent);
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED);
|
||||
intent.putExtra(SentBody.class.getName(), (SentBody) body);
|
||||
@ -211,23 +207,17 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
return channel.isActive();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void closeSession()
|
||||
{
|
||||
if(channel!=null)
|
||||
{
|
||||
public void closeSession() {
|
||||
if (channel != null) {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
|
||||
Log.i(TAG, "****************CIM连接服务器成功:"+ctx.channel().localAddress()+" NID:"+ctx.channel().id().asShortText());
|
||||
Log.i(TAG, "****************CIM连接服务器成功:" + ctx.channel().localAddress() + " NID:"
|
||||
+ ctx.channel().id().asShortText());
|
||||
|
||||
setLastHeartbeatTime(ctx.channel());
|
||||
|
||||
@ -240,7 +230,8 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) {
|
||||
|
||||
Log.e(TAG, "****************CIM与服务器断开连接:"+ctx.channel().localAddress()+" NID:"+ctx.channel().id().asShortText());
|
||||
Log.e(TAG, "****************CIM与服务器断开连接:" + ctx.channel().localAddress() + " NID:"
|
||||
+ ctx.channel().id().asShortText());
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED);
|
||||
@ -252,17 +243,15 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
|
||||
|
||||
/**
|
||||
* 用于解决,wifi情况下。偶而路由器与服务器断开连接时,客户端并没及时收到关闭事件
|
||||
* 导致这样的情况下当前连接无效也不会重连的问题
|
||||
* 用于解决,wifi情况下。偶而路由器与服务器断开连接时,客户端并没及时收到关闭事件 导致这样的情况下当前连接无效也不会重连的问题
|
||||
*
|
||||
*/
|
||||
if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state().equals(IdleState.READER_IDLE))
|
||||
{
|
||||
Log.d(TAG, "****************CIM "+IdleState.READER_IDLE+":"+ctx.channel().localAddress()+" NID:"+ctx.channel().id().asShortText());
|
||||
if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state().equals(IdleState.READER_IDLE)) {
|
||||
Log.d(TAG, "****************CIM " + IdleState.READER_IDLE + ":" + ctx.channel().localAddress() + " NID:"
|
||||
+ ctx.channel().id().asShortText());
|
||||
|
||||
Long lastTime = getLastHeartbeatTime(ctx.channel());
|
||||
if(lastTime != null && System.currentTimeMillis() - lastTime > HEARBEAT_TIME_OUT)
|
||||
{
|
||||
if (lastTime != null && System.currentTimeMillis() - lastTime > HEARBEAT_TIME_OUT) {
|
||||
channel.close();
|
||||
Log.e(TAG, "****************CIM心跳超时 ,即将重新连接......" + " NID:" + ctx.channel().id().asShortText());
|
||||
}
|
||||
@ -273,7 +262,8 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
|
||||
Log.e(TAG, "****************CIM连接出现未知异常:"+ctx.channel().localAddress()+" NID:"+ctx.channel().id().asShortText());
|
||||
Log.e(TAG, "****************CIM连接出现未知异常:" + ctx.channel().localAddress() + " NID:"
|
||||
+ ctx.channel().id().asShortText());
|
||||
|
||||
if (cause != null && cause.getMessage() != null) {
|
||||
Log.e(TAG, cause.getMessage());
|
||||
@ -311,13 +301,11 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
|
||||
}
|
||||
|
||||
private void setLastHeartbeatTime(Channel channel)
|
||||
{
|
||||
private void setLastHeartbeatTime(Channel channel) {
|
||||
channel.attr(AttributeKey.valueOf(KEY_LAST_HEART_TIME)).set(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
private Long getLastHeartbeatTime(Channel channel)
|
||||
{
|
||||
private Long getLastHeartbeatTime(Channel channel) {
|
||||
return (Long) channel.attr(AttributeKey.valueOf(KEY_LAST_HEART_TIME)).get();
|
||||
}
|
||||
|
||||
@ -326,7 +314,8 @@ class CIMConnectorManager extends SimpleChannelInboundHandler<Object> {
|
||||
ConnectivityManager nw = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo networkInfo = nw.getActiveNetworkInfo();
|
||||
return networkInfo != null;
|
||||
} catch (Exception e) {}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.android.exception.SessionClosedException;
|
||||
import com.farsunset.cim.sdk.android.model.Message;
|
||||
@ -33,6 +32,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
/**
|
||||
* 消息入口,所有消息都会经过这里
|
||||
*/
|
||||
@ -50,33 +50,30 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
*/
|
||||
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)
|
||||
|| intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)
|
||||
||intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED))
|
||||
{
|
||||
|| intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
|
||||
startPushService();
|
||||
}
|
||||
|
||||
/*
|
||||
* 设备网络状态变化事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED))
|
||||
{
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED)) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
onDevicesNetworkChanged(connectivityManager.getActiveNetworkInfo());
|
||||
}
|
||||
|
||||
/*
|
||||
* cim断开服务器事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED)) {
|
||||
onInnerConnectionClosed();
|
||||
}
|
||||
|
||||
/*
|
||||
* cim连接服务器失败事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED)) {
|
||||
long interval = intent.getLongExtra("interval", CIMConstant.RECONN_INTERVAL_TIME);
|
||||
String exceptionName = intent.getStringExtra(Exception.class.getName());
|
||||
onConnectionFailed(exceptionName, interval);
|
||||
@ -85,34 +82,28 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
/*
|
||||
* cim连接服务器成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_SUCCESSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_SUCCESSED)) {
|
||||
onInnerConnectionSuccessed();
|
||||
}
|
||||
|
||||
/*
|
||||
* 收到推送消息事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED)) {
|
||||
onInnerMessageReceived((Message) intent.getSerializableExtra(Message.class.getName()), intent);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取收到replybody成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED)) {
|
||||
onReplyReceived((ReplyBody) intent.getSerializableExtra(ReplyBody.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取sendbody发送失败事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_FAILED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_FAILED)) {
|
||||
String exceptionName = intent.getStringExtra(Exception.class.getName());
|
||||
SentBody sentBody = (SentBody) intent.getSerializableExtra(SentBody.class.getName());
|
||||
onSentFailed(exceptionName, sentBody);
|
||||
@ -121,18 +112,14 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
/*
|
||||
* 获取sendbody发送成功事件
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED)) {
|
||||
onSentSucceed((SentBody) intent.getSerializableExtra(SentBody.class.getName()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 重新连接,如果断开的话
|
||||
*/
|
||||
if(intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY))
|
||||
{
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
}
|
||||
@ -145,8 +132,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
|
||||
private void onInnerConnectionClosed() {
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE, false);
|
||||
if(CIMConnectorManager.isNetworkConnected(context))
|
||||
{
|
||||
if (CIMConnectorManager.isNetworkConnected(context)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
|
||||
@ -155,8 +141,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
|
||||
private void onConnectionFailed(String exceptionName, long reinterval) {
|
||||
|
||||
if(CIMConnectorManager.isNetworkConnected(context))
|
||||
{
|
||||
if (CIMConnectorManager.isNetworkConnected(context)) {
|
||||
onConnectionFailed();
|
||||
|
||||
CIMPushManager.connect(context, reinterval);
|
||||
@ -172,8 +157,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
|
||||
private void onDevicesNetworkChanged(NetworkInfo info) {
|
||||
|
||||
if(info !=null)
|
||||
{
|
||||
if (info != null) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
|
||||
@ -181,42 +165,35 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver{
|
||||
}
|
||||
|
||||
private void onInnerMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent) {
|
||||
if(isForceOfflineMessage(message.getAction()))
|
||||
{
|
||||
if (isForceOfflineMessage(message.getAction())) {
|
||||
CIMPushManager.stop(context);
|
||||
}
|
||||
|
||||
onMessageReceived(message, intent);
|
||||
}
|
||||
|
||||
private boolean isForceOfflineMessage(String action)
|
||||
{
|
||||
private boolean isForceOfflineMessage(String action) {
|
||||
return CIMConstant.MessageAction.ACTION_999.equals(action);
|
||||
}
|
||||
|
||||
private void onSentFailed(String exceptionName, SentBody body) {
|
||||
|
||||
// 与服务端端开链接,重新连接
|
||||
if(SessionClosedException.class.getSimpleName().equals(exceptionName))
|
||||
{
|
||||
if (SessionClosedException.class.getSimpleName().equals(exceptionName)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
// 发送失败 重新发送
|
||||
CIMPushManager.sendRequest(context, body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public abstract void onMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent);
|
||||
|
||||
public void onNetworkChanged(NetworkInfo info) {
|
||||
CIMListenerManager.notifyOnNetworkChanged(info);
|
||||
}
|
||||
|
||||
|
||||
public void onConnectionSuccessed(boolean hasAutoBind) {
|
||||
CIMListenerManager.notifyOnConnectionSuccessed(hasAutoBind);
|
||||
}
|
||||
|
@ -30,39 +30,41 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
/**
|
||||
* CIM 主要事件接口
|
||||
*/
|
||||
public interface CIMEventListener
|
||||
{
|
||||
|
||||
public interface CIMEventListener {
|
||||
|
||||
/**
|
||||
* 当收到服务端推送过来的消息时调用
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
void onMessageReceived(Message message);
|
||||
|
||||
/**
|
||||
* 当调用CIMPushManager.sendRequest()向服务端发送请求,获得相应时调用
|
||||
*
|
||||
* @param replybody
|
||||
*/
|
||||
void onReplyReceived(ReplyBody replybody);
|
||||
|
||||
/**
|
||||
* 当调用CIMPushManager.sendRequest()向服务端发送请求成功时
|
||||
*
|
||||
* @param body
|
||||
*/
|
||||
void onSentSuccessed(SentBody body);
|
||||
|
||||
|
||||
/**
|
||||
* 当手机网络发生变化时调用
|
||||
*
|
||||
* @param networkinfo
|
||||
*/
|
||||
void onNetworkChanged(NetworkInfo networkinfo);
|
||||
|
||||
|
||||
/**
|
||||
* 当连接服务器成功时回调
|
||||
* @param hasAutoBind : true 已经自动绑定账号到服务器了,不需要再手动调用bindAccount
|
||||
*
|
||||
* @param hasAutoBind
|
||||
* : true 已经自动绑定账号到服务器了,不需要再手动调用bindAccount
|
||||
*/
|
||||
void onConnectionSuccessed(boolean hasAutoBind);
|
||||
|
||||
@ -83,4 +85,3 @@ public interface CIMEventListener
|
||||
*/
|
||||
int getEventDispatchOrder();
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@ -31,7 +32,6 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
import android.net.NetworkInfo;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
/**
|
||||
* CIM 消息监听器管理
|
||||
*/
|
||||
@ -48,7 +48,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void removeMessageListener(CIMEventListener listener) {
|
||||
for (int i = 0; i < cimListeners.size(); i++) {
|
||||
if (listener.getClass() == cimListeners.get(i).getClass()) {
|
||||
@ -57,19 +56,18 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void notifyOnNetworkChanged(NetworkInfo info) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onNetworkChanged(info);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void notifyOnConnectionSuccessed(boolean hasAutoBind) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onConnectionSuccessed(hasAutoBind);
|
||||
}
|
||||
}
|
||||
|
||||
public static void notifyOnMessageReceived(Message message) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onMessageReceived(message);
|
||||
@ -88,7 +86,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void notifyOnReplyReceived(ReplyBody body) {
|
||||
for (CIMEventListener listener : cimListeners) {
|
||||
listener.onReplyReceived(body);
|
||||
@ -101,7 +98,6 @@ public class CIMListenerManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void destory() {
|
||||
cimListeners.clear();
|
||||
}
|
||||
@ -127,5 +123,4 @@ public class CIMListenerManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import android.content.Context;
|
||||
@ -52,6 +53,7 @@ public class CIMPushManager {
|
||||
|
||||
/**
|
||||
* 初始化,连接服务端,在程序启动页或者 在Application里调用
|
||||
*
|
||||
* @param context
|
||||
* @param ip
|
||||
* @param port
|
||||
@ -70,8 +72,7 @@ public class CIMPushManager {
|
||||
CIMCacheManager.putString(context, CIMCacheManager.KEY_CIM_SERVIER_HOST, ip);
|
||||
CIMCacheManager.putInt(context, CIMCacheManager.KEY_CIM_SERVIER_PORT, port);
|
||||
|
||||
if(!autoBind)
|
||||
{
|
||||
if (!autoBind) {
|
||||
CIMCacheManager.remove(context, CIMCacheManager.KEY_ACCOUNT);
|
||||
}
|
||||
|
||||
@ -89,8 +90,7 @@ public class CIMPushManager {
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if(isManualStop || isManualDestory)
|
||||
{
|
||||
if (isManualStop || isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -101,17 +101,16 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置一个账号登录到服务端
|
||||
* @param account 用户唯一ID
|
||||
*
|
||||
* @param account
|
||||
* 用户唯一ID
|
||||
*/
|
||||
public static void bindAccount(Context context, String account) {
|
||||
|
||||
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if(isManualDestory || account==null || account.trim().length()==0)
|
||||
{
|
||||
if (isManualDestory || account == null || account.trim().length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -119,7 +118,6 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static void sendBindRequest(Context context, String account) {
|
||||
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, false);
|
||||
@ -147,8 +145,7 @@ public class CIMPushManager {
|
||||
|
||||
String account = CIMCacheManager.getString(context, CIMCacheManager.KEY_ACCOUNT);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if( account==null || account.trim().length()==0 || isManualDestory )
|
||||
{
|
||||
if (account == null || account.trim().length() == 0 || isManualDestory) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -157,10 +154,9 @@ public class CIMPushManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 发送一个CIM请求
|
||||
*
|
||||
* @param context
|
||||
* @body
|
||||
*/
|
||||
@ -169,8 +165,7 @@ public class CIMPushManager {
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if(isManualStop || isManualDestory)
|
||||
{
|
||||
if (isManualStop || isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -183,6 +178,7 @@ public class CIMPushManager {
|
||||
|
||||
/**
|
||||
* 停止接受推送,将会退出当前账号登录,端口与服务端的连接
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void stop(Context context) {
|
||||
@ -200,14 +196,13 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 完全销毁CIM,一般用于完全退出程序,调用resume将不能恢复
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void destroy(Context context) {
|
||||
|
||||
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED, true);
|
||||
CIMCacheManager.putString(context, CIMCacheManager.KEY_ACCOUNT, null);
|
||||
|
||||
@ -217,9 +212,9 @@ public class CIMPushManager {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重新恢复接收推送,重新连接服务端,并登录当前账号
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void resume(Context context) {
|
||||
@ -236,8 +231,6 @@ public class CIMPushManager {
|
||||
return CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static String getVersionName(Context context) {
|
||||
String versionName = null;
|
||||
try {
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
@ -28,9 +29,9 @@ import android.os.Message;
|
||||
import android.util.Log;
|
||||
import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
|
||||
|
||||
/**
|
||||
* 与服务端连接服务
|
||||
*
|
||||
* @author 3979434
|
||||
*
|
||||
*/
|
||||
@ -38,13 +39,12 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
private final String TAG = CIMPushService.class.getSimpleName();
|
||||
public final static String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
|
||||
private CIMConnectorManager manager;
|
||||
|
||||
@Override
|
||||
public void onCreate()
|
||||
{
|
||||
public void onCreate() {
|
||||
manager = CIMConnectorManager.getManager(this.getApplicationContext());
|
||||
}
|
||||
|
||||
|
||||
Handler connectionHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(android.os.Message message) {
|
||||
@ -57,17 +57,14 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
|
||||
intent = (intent == null ? new Intent(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE) : intent);
|
||||
|
||||
String action = intent.getAction();
|
||||
|
||||
if(CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
|
||||
|
||||
long delayMillis = intent.getLongExtra(KEY_DELAYED_TIME, 0);
|
||||
if (delayMillis > 0) {
|
||||
@ -77,46 +74,40 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
msg.setData(intent.getExtras());
|
||||
connectionHandler.sendMessageDelayed(msg, delayMillis);
|
||||
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
String host = intent.getStringExtra(CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = intent.getIntExtra(CIMCacheManager.KEY_CIM_SERVIER_PORT, 0);
|
||||
manager.connect(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action)) {
|
||||
manager.send((SentBody) intent.getSerializableExtra(CIMPushManager.KEY_SEND_BODY));
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action)) {
|
||||
manager.closeSession();
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_DESTORY.equals(action))
|
||||
{
|
||||
if (CIMPushManager.ACTION_DESTORY.equals(action)) {
|
||||
manager.destroy();
|
||||
this.stopSelf();
|
||||
}
|
||||
|
||||
if(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action) )
|
||||
{
|
||||
if (CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action)) {
|
||||
if (!manager.isConnected()) {
|
||||
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(getApplicationContext(),CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(getApplicationContext(),
|
||||
CIMCacheManager.KEY_MANUAL_STOP);
|
||||
Log.w(TAG, "manager.isConnected() == false, isManualStop == " + isManualStop);
|
||||
CIMPushManager.connect(this, 0);
|
||||
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
Log.i(TAG, "manager.isConnected() == true");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ public interface CIMConstant {
|
||||
|
||||
// 消息头长度为3个字节,第一个字节为消息类型,第二,第三字节 转换int后为消息长度
|
||||
int DATA_HEADER_LENGTH = 3;
|
||||
|
||||
public static interface ReturnCode {
|
||||
|
||||
String CODE_404 = "404";
|
||||
@ -44,11 +45,8 @@ public interface CIMConstant {
|
||||
|
||||
String CODE_500 = "500";
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static interface ProtobufType {
|
||||
byte C_H_RS = 0;
|
||||
byte S_H_RQ = 1;
|
||||
@ -59,7 +57,6 @@ public interface CIMConstant {
|
||||
|
||||
public static interface RequestKey {
|
||||
|
||||
|
||||
String CLIENT_BIND = "client_bind";
|
||||
|
||||
String CLIENT_LOGOUT = "client_logout";
|
||||
@ -69,14 +66,12 @@ public interface CIMConstant {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static interface MessageAction {
|
||||
|
||||
// 被其他设备登录挤下线消息
|
||||
String ACTION_999 = "999";
|
||||
}
|
||||
|
||||
|
||||
public static interface IntentAction {
|
||||
|
||||
// 消息广播action
|
||||
|
@ -41,6 +41,7 @@ import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
*/
|
||||
public class ClientMessageDecoder extends ByteToMessageDecoder {
|
||||
final static String TAG = ClientMessageDecoder.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception {
|
||||
|
||||
|
@ -21,7 +21,6 @@
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android.filter;
|
||||
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
import com.farsunset.cim.sdk.android.model.Protobufable;
|
||||
|
||||
@ -31,7 +30,6 @@ import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端消息发送前进行编码,可在此加密消息
|
||||
*
|
||||
@ -41,13 +39,11 @@ public class ClientMessageEncoder extends MessageToByteEncoder<Object> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, Object message, ByteBuf out) throws Exception {
|
||||
|
||||
|
||||
if (message instanceof Protobufable) {
|
||||
|
||||
Protobufable data = (Protobufable) message;
|
||||
byte[] byteArray = data.getByteArray();
|
||||
|
||||
|
||||
out.writeBytes(createHeader(data.getType(), byteArray.length));
|
||||
out.writeBytes(byteArray);
|
||||
|
||||
@ -58,6 +54,7 @@ public class ClientMessageEncoder extends MessageToByteEncoder<Object> {
|
||||
|
||||
/**
|
||||
* 消息体最大为65535
|
||||
*
|
||||
* @param type
|
||||
* @param length
|
||||
* @return
|
||||
@ -70,7 +67,4 @@ public class ClientMessageEncoder extends MessageToByteEncoder<Object> {
|
||||
return header;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ package com.farsunset.cim.sdk.android.model;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
|
||||
/**
|
||||
* 服务端心跳请求
|
||||
*
|
||||
@ -53,7 +54,6 @@ public class HeartbeatRequest implements Serializable,Protobufable {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte getType() {
|
||||
return CIMConstant.ProtobufType.S_H_RQ;
|
||||
|
@ -24,6 +24,7 @@ package com.farsunset.cim.sdk.android.model;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
|
||||
/**
|
||||
* 客户端心跳响应
|
||||
*/
|
||||
@ -42,6 +43,7 @@ public class HeartbeatResponse implements Serializable,Protobufable {
|
||||
public static HeartbeatResponse getInstance() {
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getByteArray() {
|
||||
return CMD_HEARTBEAT_RESPONSE.getBytes();
|
||||
|
@ -30,13 +30,11 @@ public class Message implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
private String mid;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型,用户自定义消息类别
|
||||
*/
|
||||
@ -59,8 +57,6 @@ public class Message implements Serializable {
|
||||
*/
|
||||
private String receiver;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* content 内容格式
|
||||
*/
|
||||
@ -73,11 +69,10 @@ public class Message implements Serializable {
|
||||
|
||||
private long timestamp;
|
||||
|
||||
|
||||
public Message()
|
||||
{
|
||||
public Message() {
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -86,14 +81,14 @@ public class Message implements Serializable {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@ -126,7 +121,6 @@ public class Message implements Serializable {
|
||||
this.receiver = receiver;
|
||||
}
|
||||
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
@ -135,14 +129,14 @@ public class Message implements Serializable {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getExtra() {
|
||||
return extra;
|
||||
}
|
||||
|
||||
public void setExtra(String extra) {
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
@ -159,7 +153,6 @@ public class Message implements Serializable {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
public String getMid() {
|
||||
return mid;
|
||||
}
|
||||
@ -172,5 +165,4 @@ public class Message implements Serializable {
|
||||
return txt != null && txt.trim().length() != 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
***************************************************************************************
|
||||
*/
|
||||
package com.farsunset.cim.sdk.android.model;
|
||||
|
||||
/**
|
||||
* 需要向另一端发送的结构体
|
||||
*/
|
||||
|
@ -39,7 +39,6 @@ public class ReplyBody implements Serializable {
|
||||
*/
|
||||
private String key;
|
||||
|
||||
|
||||
/**
|
||||
* 返回码
|
||||
*/
|
||||
@ -50,16 +49,13 @@ public class ReplyBody implements Serializable {
|
||||
*/
|
||||
private String message;
|
||||
|
||||
|
||||
private long timestamp;
|
||||
|
||||
|
||||
/**
|
||||
* 返回数据集合
|
||||
*/
|
||||
private Hashtable<String, String> data = new Hashtable<String, String>();
|
||||
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
@ -68,8 +64,6 @@ public class ReplyBody implements Serializable {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
@ -114,9 +108,7 @@ public class ReplyBody implements Serializable {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#ReplyBody#").append("\n");
|
||||
buffer.append("key:").append(this.getKey()).append("\n");
|
||||
@ -125,8 +117,7 @@ public class ReplyBody implements Serializable {
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
|
@ -73,12 +73,10 @@ public class SentBody implements Serializable,Protobufable {
|
||||
data.put(k, v);
|
||||
}
|
||||
|
||||
|
||||
public Set<String> getKeySet() {
|
||||
return data.keySet();
|
||||
}
|
||||
|
||||
|
||||
public void remove(String k) {
|
||||
data.remove(k);
|
||||
}
|
||||
@ -86,13 +84,13 @@ public class SentBody implements Serializable,Protobufable {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#SentBody#").append("\n");;
|
||||
buffer.append("#SentBody#").append("\n");
|
||||
;
|
||||
buffer.append("key:").append(key).append("\n");
|
||||
buffer.append("timestamp:").append(timestamp).append("\n");
|
||||
if (!data.isEmpty()) {
|
||||
buffer.append("data{").append("\n");
|
||||
for(String key:getKeySet())
|
||||
{
|
||||
for (String key : getKeySet()) {
|
||||
buffer.append(key).append(":").append(this.get(key)).append("\n");
|
||||
}
|
||||
buffer.append("}");
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -23,7 +23,7 @@ package com.farsunset.cim.sdk.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
class CIMCacheToolkit {
|
||||
class CIMCacheManager {
|
||||
|
||||
private static HashMap<String, String> CIM_CONFIG_INFO = new HashMap<String, String>();
|
||||
|
||||
@ -37,53 +37,42 @@ class CIMCacheToolkit {
|
||||
|
||||
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
|
||||
|
||||
static CIMCacheToolkit toolkit;
|
||||
public static CIMCacheToolkit getInstance(){
|
||||
static CIMCacheManager toolkit;
|
||||
|
||||
public static CIMCacheManager getInstance() {
|
||||
if (toolkit == null) {
|
||||
toolkit = new CIMCacheToolkit();
|
||||
toolkit = new CIMCacheManager();
|
||||
}
|
||||
return toolkit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void remove(String key)
|
||||
{
|
||||
public void remove(String key) {
|
||||
CIM_CONFIG_INFO.remove(key);
|
||||
}
|
||||
|
||||
|
||||
public void putString(String key,String value)
|
||||
{
|
||||
public void putString(String key, String value) {
|
||||
CIM_CONFIG_INFO.put(key, value);
|
||||
|
||||
}
|
||||
|
||||
public String getString(String key)
|
||||
{
|
||||
public String getString(String key) {
|
||||
return CIM_CONFIG_INFO.get(key);
|
||||
}
|
||||
|
||||
public void putBoolean(String key,boolean value)
|
||||
{
|
||||
public void putBoolean(String key, boolean value) {
|
||||
putString(key, Boolean.toString(value));
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key)
|
||||
{
|
||||
public boolean getBoolean(String key) {
|
||||
String value = getString(key);
|
||||
return value == null ? false : Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
|
||||
public void putInt(String key,int value)
|
||||
{
|
||||
public void putInt(String key, int value) {
|
||||
putString(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
public int getInt(String key)
|
||||
{
|
||||
public int getInt(String key) {
|
||||
String value = getString(key);
|
||||
return value == null ? 0 : Integer.parseInt(value);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user