From:http://blog.csdn.net/li352558693/article/details/42639683
使用java-websocket实现即时通讯/推送模块;
支持即时发消息和收消息,消息类型有开发者自定义;;
该模块需要websocket.jar的支持;开源项目地址--https://github.com/TooTallNate/Java-WebSocket
该开源项目支持客户端client和服务端server的配置使用,并提供示例test;
1,Android 客户端使用需要配置网络权限;
2,需要写一个自己的client类来继承WebsocketClient;实现websocket的状态回调和新消息的解析动作;
3,需要监控自己的client的链接状态,维持长链接;
4,发送和接收
下面贴出部分相关代码;
网络权限的不用说了吧!
client类:
常用状态回调方法有4个;
自己可以在对应的函数里面做响应的处理,比如当链接发生错误时要重新去打开该链接,收到消息时即时的保存聊天记录和发送系统通知来提醒用户查看新消息等等;
- public class TestClient extends WebSocketClient {
- public TestClient(URI serverURI) {
- super(serverURI);
- }
- /***
- * 链接关闭
- */
- @Override
- public void onClose(int arg0, String arg1, boolean arg2) {
- }
- /***
- * 链接发生错误
- */
- @Override
- public void onError(Exception arg0) {
- }
- /**
- * 新消息
- */
- @Override
- public void onMessage(String arg0) {
- }
- /***
- * 链接打开
- */
- @Override
- public void onOpen(ServerHandshake arg0) {
- // TODO Auto-generated method stub
- }
- }
下面是我聊天部分代码,有离线消息/PC同步/多对多的聊天;
仅供参考; copy
- /***
- * <h2>WebSocket Android 客户端</h2>
- * <ol>
- * <li>Socket链接打开回调 {@link WebSocket#onOpen(ServerHandshake)},此处有
- * {@link SocketConstant#ON_OPEN} 广播发出;
- * <li>Socket链接出现异常错误时回调 {@link WebSocket#onError(Exception)},此处有
- * {@link SocketConstant#ON_ERROR}广播发出;
- * <li>Socket链接关闭回调 {@link WebSocket #onClose(int, String, boolean)},此处有
- * {@link SocketConstant#ON_CLOSES}广播发出;
- * <li>Socket链接接收消息回调 {@link WebSocket#onMessage(String)}
- * ,此处做收消息的逻辑的处理;包括发送消息服务器返回的发送结果,PC端同步接收的新消息,及外来的新消息;
- * <li>检测是否有消息遗漏 {@link WebSocket#checkMsgWebId(String, String)},参数为联系人和webId;
- * <li>取得正在桌面运行的activity的名称 {@link WebSocket#getRunningActivityName()}
- * <li>接收到的消息的处理 {@link WebSocket#messageHandle(MessageEntity, String)}
- * ,参数为消息实体和消息类型 (buyer/server)
- * <li>发送新消息的系统通知 {@link WebSocket#sendNotification(String)},参数为联系人;
- * <li>保存离线消息 {@link WebSocket#saveOffLineMsg(HashMap)},参数为接收到的离线消息集合;
- * <li>保存从服务端获取的联系人的webId {@link WebSocket#saveContactsWebID(HashMap)}
- * ,参数为以联系人为key以最大webId为值得map集合;
- * </ol>
- *
- * @author li'mingqi <a> 2014-3-19</a>
- *
- */
- public class WebSocket extends WebSocketClient {
- // 登陆返回的back_type字段
- public static final String LOGIN_RETURN_TYPE = "login";
- // 发送信息的back_type字段
- public static final String SEND_RETURN_TYPE = "send_result";
- // 接收信息的back_type字段
- public static final String RECEIVER_RETURN_TYPE = "msg";
- // 接收客服的信息的back_type字段
- public static final String GET_SERVER_RETURN_TYPE = "server_info";
- // 接收服务端返回对应联系人的最大顺序ID
- public static final String CONTACTS_MAX_WEBID_TYPE = "max_id_return";
- // 接收用户的离线消息
- public static final String USER_OFFLINE_MSG_TYPE = "offline";
- // 上下文对象
- private Context mContext;
- // socket返回json解析类对象
- private WebSocketParser mParser;
- // 系统通知管理
- public NotificationManager mNotificationManager;
- // 系统通知
- private Notification mNoti;
- // 意图
- private PendingIntent mIntent;
- // 该系统通知的 id
- public static final int NOTIFICATION_ID = 100;
- @SuppressWarnings("deprecation")
- public SGWebSocket(Context context, URI serverUri, Draft draft) {
- super(serverUri, draft);
- this.mContext = context;
- //新消息的解析类
- this.mParser = WebSocketParser.getInstance();
- //收到新消息发送的通知
- this.mNotificationManager = (NotificationManager) this.mContext
- .getSystemService(Context.NOTIFICATION_SERVICE);
- this.mNoti = new Notification(R.drawable.system_info, "您有新消息!",
- System.currentTimeMillis());
- }
- /***
- * send broadcast <SGSocketConstant>ON_CLOSES filter if this socket closed
- * socket 发生关闭时发送的广播,若想提示,可以接受并处理
- *
- */
- @Override
- public void onClose(int arg0, String arg1, boolean arg2) {
- // 更改保存的链接状态
- UserInfoUtil.saveSocket(mContext, false);
- mNotificationManager.cancelAll();
- Intent intent = new Intent(SocketConstant.ON_CLOSES);
- intent.putExtra(SocketConstant.ON_CLOSES, arg1.toString());
- mContext.sendBroadcast(intent);
- }
- /***
- * send broadcast <SGSocketConstant>ON_ERROR filter if this socket has error
- * socket 发生错误发送的广播,若想提示,可以接受并处理
- *
- */
- @Override
- public void onError(Exception arg0) {
- Intent intent = new Intent(SGSocketConstant.ON_ERROR);
- intent.putExtra(SGSocketConstant.ON_ERROR, arg0.toString());
- mContext.sendBroadcast(intent);
- this.close();
- }
- // 买家
- public static final String MSG_BUYER_TYPE = "1";
- // 客服
- public static final String MSG_SERVCER_TYPE = "2";
- // 游客
- // public static final String MSG_RANDOM_TYPE = "3";
- /***
- * receiver message from server 1,登陆返回 type
- * <WebSocket>LOGIN_RETURN_TYPE; 2,发送返回 type
- * <WebSocket>SEND_RETURN_TYPE; 3,接收信息返回 type
- * <WebSocket>RECEIVER_RETURN_TYPE;
- *
- * @throws InterruptedException
- */
- @Override
- public void onMessage(String content) {
- // parser
- try {
- JSONObject object = new JSONObject(content);
- Log.i("json", "卖家--" + object.toString());
- String back_type = object.getString("back_type");
- String activity = getRunningActivityName();
- if (SEND_RETURN_TYPE.equals(back_type)) {// 发送具体消息时返回发送结果
- // json解析
- MessageEntity entity = mParser.sendMessageParser(mContext,
- content);
- if ("true".equals(entity.getSend_state())) {// 发送成功
- // 判断是否是PC端发送的消息,若是PC端发送的消息,则在Android端做同步存储处理
- // 1,首先判断数据库中是否包含该条信息
- boolean has = MessageDB.getInstance(mContext)
- .findMessageByMsgId(entity.get_id());
- if (has) {
- // Android端发送
- MessageDB.getInstance(mContext).update(entity.get_id(),
- true, entity.getReceiverTime(),
- entity.getWebId());// 更新发送状态为已发送
- } else {
- // PC端发送,将该消息同步到Android端数据库
- entity.setSend_state(SocketConstant.MSG_SEND_SUCCESS_STATE);
- MessageDB.getInstance(mContext).insert(entity,
- SocketConstant.MSG_TYPE_BUYER);// 卖家发送给买家的
- // 通知聊天主页面,更新聊天列表
- pcSynAndroid(activity);
- }
- // 检测是否有消息遗漏
- checkMsgWebId(entity.getContacts(), entity.getWebId());
- Log.i("miss", "发送返回或者PC同步--" + entity.getContacts() + "--"
- + entity.getWebId());
- } else if ("false".equals(entity.getSend_state())) {// 发送失败
- MessageDB.getInstance(mContext).update(entity.get_id(),
- false, entity.getReceiverTime(), entity.getWebId());
- Toast.makeText(mContext, entity.getErrorText(),
- Toast.LENGTH_SHORT).show();
- }
- // 登陆返回 记录session
- } else if (LOGIN_RETURN_TYPE.equals(back_type)) {
- KApplication.session = object.getString("session_id");
- String str = object.getString("login_status");
- if ("true".equals(str)) {
- UserInfoUtil.saveSocket(mContext, true);
- // 生成json请求字符串
- String maxIdstring = SocketJsonUtil
- .getContactsCurrentWebId(UserInfoUtil
- .getUser(mContext)[0], "2", MessageDB
- .getInstance(mContext)
- .findAllContactsAndType());
- // 登陆成功,向服务器索取联系人的最大webId
- send(maxIdstring);
- Log.i("send", maxIdstring);
- } else if ("false".equals(str)) {
- UserInfoUtil.saveSocket(mContext, false);
- }
- } else if (RECEIVER_RETURN_TYPE.equals(back_type)) {// 接收到的具体聊天的信息
- // json解析
- MessageEntity entity = mParser.receiverMessagePrser(mContext,
- content);
- // 判断数据库中是否有该条消息,有则不处理,无则处理消息;
- if (!MessageDB.getInstance(mContext).findMessageByMsgId(
- entity.get_id())) {
- // 消息处理
- if (MSG_BUYER_TYPE.equals(entity.getSenderType())) {
- // 买家
- messageHandle(entity, SocketConstant.MSG_TYPE_BUYER);
- } else if (MSG_SERVCER_TYPE.equals(entity.getSenderType())) {
- // 卖家,客服
- messageHandle(entity, SocketConstant.MSG_TYPE_SERVER);
- }
- Log.i("miss", "没有该条消息");
- // 检测是否有消息遗漏
- checkMsgWebId(entity.getContacts(), entity.getWebId());
- }
- } else if (GET_SERVER_RETURN_TYPE.equals(back_type)) {// 获取闪聊客服返回的数据
- // 客服
- ServerEntity entity = mParser.serverInfoParser(content);// 客服对象
- Intent intent = new Intent(SocketConstant.GET_SERVER_INFO);
- intent.putExtra("server_info", entity);
- mContext.sendBroadcast(intent);
- } else if (CONTACTS_MAX_WEBID_TYPE.equals(back_type)) {
- // 返回的联系人最大的消息id
- HashMap<String, String> map = mParser.contactsMaxWebId(content);
- // 将联系人和其最大webId存入临时集合
- saveContactsWebID(map);
- // 开始请求服务器,释放离线消息给客户端;
- send(SocketJsonUtil.getOffLine(
- UserInfoUtil.getUser(mContext)[0], "2"));
- Log.i("send",
- SocketJsonUtil.getOffLine(
- UserInfoUtil.getUser(mContext)[0], "2"));
- } else if (USER_OFFLINE_MSG_TYPE.equals(back_type)) {
- // 用户的离线消息
- HashMap<String, ArrayList<MessageEntity>> map = mParser
- .offLineMsg(mContext, content);
- // 将离线消息入库
- saveOffLineMsg(map);
- }
- } catch (JSONException e) {
- this.close();
- }
- }
- /***
- * send broadcast <SocketConstant>ON_OPEN filter if this socket opened
- * socket 打开时发送的广播,若想提示,可以接受并处理
- *
- */
- @Override
- public void onOpen(ServerHandshake arg0) {
- Intent intent = new Intent(SGSocketConstant.ON_OPEN);
- mContext.sendBroadcast(intent);
- }
- /***
- * 检测正在运行tasktop的activity
- * @return current running activity name
- *
- */
- private String getRunningActivityName() {
- ActivityManager activityManager = (ActivityManager) mContext
- .getSystemService(Context.ACTIVITY_SERVICE);
- String runningActivity = activityManager.getRunningTasks(1).get(0).topActivity
- .getClassName();
- return runningActivity;
- }
- /***
- * send notification for this contacts
- * 发送通知
- * @param contacts
- *
- */
- @SuppressWarnings("deprecation")
- private void sendNotification(String contacts) {
- Intent intent = new Intent(mContext, MainActivity.class);
- mIntent = PendingIntent.getActivity(mContext, 100, intent, 0);
- mNoti.flags = Notification.FLAG_AUTO_CANCEL;
- mNoti.defaults = Notification.DEFAULT_VIBRATE;
- mNoti.setLatestEventInfo(mContext, "标题", "您有新消息!", mIntent);
- mNoti.contentView = new RemoteViews(mContext.getApplicationContext()
- .getPackageName(), R.layout.notification_item);
- mNoti.contentView.setTextViewText(R.id.noti_message, "收到来自" + contacts
- + "的新消息");
- mNotificationManager.notify(NOTIFICATION_ID, mNoti);
- }
- /***
- * 具体聊天收到的外来消息处理
- *
- * @param entity
- * 消息实体
- * @param messageType
- * 消息类型(买家/客服)
- */
- private void messageHandle(MessageEntity entity, String messageType) {
- String activity = getRunningActivityName();
- // 处于聊天的页面
- if ("com.ui.activity.ManageChartActivity".equals(activity)) {
- // 处于正在聊天对象的页面,将数据写入数据库,并发送广播更新页面数据
- if (KApplication.crurentContacts.equals(entity.getContacts())) {
- /**
- * 接收到的消息,消息实体entity的send_state字段状态设置为
- * MSG_SEND_SUCCESS_STATE(即201)
- **/
- entity.setSend_state(SocketConstant.MSG_SEND_SUCCESS_STATE);// 收到的信息,设置信息的状态
- entity.setRead(SocketConstant.READ_STATE);
- MessageDB.getInstance(mContext).insert(entity, messageType);// 将数据写入数据库,
- Intent intent = new Intent(SocketConstant.NEW_MESSAGE);
- intent.putExtra("newmsg", entity);
- mContext.sendBroadcast(intent);
- // 没有处于闪聊对象的页面,将数据写入数据库,发送系统通知
- } else {
- entity.setSend_state(SocketConstant.MSG_SEND_SUCCESS_STATE);// 收到的信息,设置信息的状态
- entity.setRead(SocketConstant.DEFAULT_READ_STATE);
- MessageDB.getInstance(mContext).insert(entity, messageType);
- if (KApplication.sp.getBoolean(RefreshUtils.noteFlag, false)) {
- sendNotification(entity.getContacts());
- }
- Intent intent = new Intent(
- SocketConstant.RECEIVER_NEW_MESSAGE);
- mContext.sendBroadcast(intent);
- }
- // 将数据写入数据库,发送系统通知
- } else {
- entity.setSend_state(SocketConstant.MSG_SEND_SUCCESS_STATE);// 收到的信息,设置信息的状态
- entity.setRead(SocketConstant.DEFAULT_READ_STATE);
- MessageDB.getInstance(mContext).insert(entity, messageType);
- Intent intent = new Intent();
- if ("com.ui.activity.ManageConversationActivity"
- .equals(activity)
- || "com.ui.activity.MainActivity"
- .equals(activity)) {
- intent.setAction(SocketConstant.RECEIVER_NEW_MESSAGE);// 聊天页面
- } else {
- intent.setAction(SocketConstant.RECEIVER_NEW_MESSAGE_OTHER);// 其他页面
- }
- mContext.sendBroadcast(intent);
- if (KApplication.sp.getBoolean(RefreshUtils.noteFlag, false)) {
- sendNotification(entity.getContacts());
- }
- }
- }
- /***
- * 电脑与手机同步信息
- *
- * @param currentActivity
- */
- public void pcSynAndroid(String currentActivity) {
- if ("com.iflashseller.ui.activity.ManageChartActivity"
- .equals(currentActivity)) {
- // 正好与该联系人对话的页面
- Intent intent = new Intent(SocketConstant.CHART_ACTIVITY);
- mContext.sendBroadcast(intent);
- } else {
- // 其他页面
- Intent intent = new Intent(SocketConstant.GROUPS_ACTIVITY);
- mContext.sendBroadcast(intent);
- }
- }
- /***
- * 检测是否有消息遗漏
- *
- * @param contacts
- * 联系人
- * @param webId
- * 服务端给出的消息Id
- */
- public void checkMsgWebId(String contacts, int webId) {
- // 集合中含有该联系人
- if (KApplication.webIds.containsKey(contacts)) {
- Log.i("miss", "保存的--" + KApplication.webIds.get(contacts));
- // 临时集合中保存的webId
- int c = KApplication.webIds.get(contacts);
- /***
- * 如果新收到的消息的webId大于临时集合中保存的改联系人的webId,且他们之间的差值大于1,
- * 则请求服务器推送疑似丢失的webId对应的消息
- */
- if (webId > c && (webId - 1) != c) {
- // id不连续
- for (int i = c + 1; i < webId; i++) {
- // 向服务器发送请求,获取遗漏的消息
- String miss = SocketJsonUtil.getMissMsg(
- UserInfoUtil.getUser(mContext)[0], "2", contacts,
- "1", i + "");
- this.send(miss);
- Log.i("miss", miss);
- }
- /***
- * 如果他们之间的差值正好为1,则修改临时集合的改联系人的webId,
- */
- } else if (webId > c && (webId - 1) == c) {
- KApplication.webIds.put(contacts, webId);
- Log.i("miss", "修改的--" + contacts + "--" + webId);
- }
- /****
- * 临时集合中没有改联系人的信息,则将该联系人的webId存入临时集合.
- */
- } else {
- KApplication.webIds.put(contacts, webId);
- Log.i("miss", "新增--" + contacts + "--" + webId);
- }
- }
- /***
- * 将从服务端获取的联系人的webId存入临时集合
- *
- * @param map
- */
- public void saveContactsWebID(HashMap<String, String> map) {
- Iterator<Entry<String, String>> iter = map.entrySet().iterator();
- while (iter.hasNext()) {
- Entry<String, String> es = iter.next();
- String contacts = es.getKey();
- String maxWebID = es.getValue();
- KApplication.webIds.put(contacts, Integer.parseInt(maxWebID));
- }
- }
- /***
- * 将离线消息入库
- *
- * @param map
- */
- public void saveOffLineMsg(HashMap<String, ArrayList<MessageEntity>> map) {
- Iterator<Entry<String, ArrayList<MessageEntity>>> iter = map.entrySet()
- .iterator();
- while (iter.hasNext()) {
- ArrayList<MessageEntity> msgs = iter.next().getValue();
- for (int i = 0; i < msgs.size(); i++) {
- threadSleep(100);
- MessageDB.getInstance(mContext).insert(msgs.get(i),
- SocketConstant.MSG_TYPE_BUYER);
- Log.i("write", "离线数据入库---" + msgs.get(i).toString());
- }
- /***
- * 如果服务端一次释放的离线消息大于等于10条,则继续请求释放离线消息.
- */
- if (msgs.size() >= 10) {
- send(SocketJsonUtil.getOffLine(
- UserInfoUtil.getUser(mContext)[0], "2"));
- Log.i("send",
- SocketJsonUtil.getOffLine(
- UserInfoUtil.getUser(mContext)[0], "2"));
- }
- }
- // 一轮消息入库结束,发送通知,更新UI;
- mContext.sendBroadcast(new Intent(
- SocketConstant.OFFLINE_MSG_RECEIVER_SUCCESS));
- }
- private void threadSleep(long time) {
- try {
- Thread.currentThread();
- Thread.sleep(time);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
下面贴出部分监控链接状态的代码,以保证能即时的收到消息;
- public class LApplication extends Application {
- public static String TAG = LApplication.class.getSimpleName();
- /** 接收消息广播 **/
- private LPullReceiver mPullReceiver;
- /** 是否是正在登录 **/
- public static boolean isLoging = false;
- /** socket管理类 **/
- private LPushManager mWebSocket;
- @Override
- public void onCreate() {
- super.onCreate();
- /***
- * 注册接收消息的广播
- */
- mPullReceiver = new LPullReceiver();
- // 广播过滤
- IntentFilter filter = new IntentFilter();
- // 时钟信息发生变化
- filter.addAction(Intent.ACTION_TIME_TICK);
- // 开机广播
- filter.addAction(Intent.ACTION_BOOT_COMPLETED);
- // 网络状态发生变化
- filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
- // 屏幕打开
- filter.addAction(Intent.ACTION_SCREEN_ON);
- // 注册广播
- registerReceiver(mPullReceiver, filter);
- // 实例化socket管理类
- mWebSocket = new LPushManager(getApplicationContext());
- // 应用重启一次,默认socket为关闭状态
- LPushUser.saveSocket(getApplicationContext(), false);
- // 默认链接没有被拒绝
- LPushUser.saveConnect(getApplicationContext(), false);
- // 1,获取当前时间
- long currentTime = System.currentTimeMillis();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = new Date(currentTime);
- String time = sdf.format(date);
- // 修改标记的时间,保证5分钟内链接一次
- LPushUser.saveOpenTime(getApplicationContext(), time);
- }
- /**
- * 广播接口类
- * <ol>
- * <li>接收时钟发生变化的广播
- * <li>接收网络发生变化的广播
- * <li>接收开机发送广播
- * <li>接收用户登录后发送的广播
- * </ol>
- *
- * @author li'mingqi
- *
- */
- class LPullReceiver extends BroadcastReceiver {
- @SuppressLint("SimpleDateFormat")
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (Intent.ACTION_TIME_TICK.equals(action)
- || ConnectivityManager.CONNECTIVITY_ACTION.equals(action)
- || Intent.ACTION_BOOT_COMPLETED.equals(action)
- || Intent.ACTION_SCREEN_ON.equals(action)) {
- if (LPushUser.GETLOG(getApplicationContext()))
- Log.i("lpush",
- "socket的链接状态----"
- + LPushUser
- .getSocket(getApplicationContext())
- + "是否正在链接--" + isLoging + "----" + action);
- // 当时钟或者网络发生变化或者socket关闭或者发生异常时或者开机 时,判断socket连接是否出现异常
- if (!LPushUser.getSocket(getApplicationContext()) && !isLoging
- && LPushUser.getSocketEnable(getApplicationContext())
- && !"".equals(IP) && !"".equals(PORT)
- && !LPushUser.getConnectState(getApplicationContext())) {
- // 掉线,执行登录动作
- if (LNetworkUtil.netIsEnable(getApplicationContext())) {
- mWebSocket.secondMethod(IP, PORT);
- // 开始登录了,标记打开时间
- // 1,获取当前时间
- long currentTime = System.currentTimeMillis();
- SimpleDateFormat sdf = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss");
- Date date = new Date(currentTime);
- String time = sdf.format(date);
- // 修改标记的时间,保证5分钟嗅探链接一次
- LPushUser.saveOpenTime(getApplicationContext(), time);
- }
- } else {
- // APP端已经处于链接正常状态 -----5分钟嗅探链接一次
- // 1,获取当前时间
- long currentTime = System.currentTimeMillis();
- SimpleDateFormat sdf = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss");
- Date date = new Date(currentTime);
- String time = sdf.format(date);
- // 2,比对链接打开时间
- long minTime = LStringManager.dateDifference(
- LPushUser.getOpenTime(getApplicationContext()),
- time);
- if (LPushUser.GETLOG(getApplicationContext())) {
- Log.i("lpush",
- "链接时长----现在时间:"
- + time
- + ";保存时间:"
- + LPushUser
- .getOpenTime(getApplicationContext())
- + ";时差" + minTime + "分钟");
- }
- if (minTime >= 5) {
- // 大于等于5分钟,则重新链接
- // 5分钟之后重新链接
- // 修改被拒绝状态
- if (LPushUser.getConnectState(getApplicationContext())) {
- LPushUser.saveConnect(getApplicationContext(),
- false);
- }
- if (LNetworkUtil.netIsEnable(getApplicationContext())
- && LPushUser
- .getSocketEnable(getApplicationContext())) {
- mWebSocket.secondMethod(IP, PORT);
- // 修改标记的时间,保证5分钟嗅探链接一次
- LPushUser.saveOpenTime(getApplicationContext(),
- time);
- }
- }
- }
- }
- }
- }
- /***
- * 设置推送功能的使用与否,默认使用推送功能,若是关闭推送功能请设置false;
- *
- * @param enable
- * 是否使用
- *
- * li'mingqi
- */
- protected void setLPushEnable(boolean enable) {
- LPushUser.saveSocketEnable(getApplicationContext(), enable);
- }
- /***
- *
- *
- * @param ip
- * ip信息
- * @param port
- * 端口信息
- *
- * li'mingqi
- */
- protected void setSocketIPInfo(String ip, String port) {
- this.IP = ip;
- this.PORT = port;
- }
- /**
- * 设置用户的Uid
- *
- * @param uid
- * li'mingqi
- */
- public void setUserInfo(int uid, String code) {
- /***
- * 数据验证
- */
- // if (0 == uid || null == code || "".equals(code)) {
- // Log.e(TAG, "您输入的用户ID或者CODE值为空");
- // new NullPointerException("您输入的用户ID或者CODE值为空").printStackTrace();
- // return;
- // }
- // 保存用户ID
- LPushUser.saveUserID(getApplicationContext(), uid);
- // 保存用户CODE
- LPushUser.saveUserCode(getApplicationContext(), code);
- // 重启链接
- mWebSocket.close();
- }
- /***
- * 设置是否查看日志
- *
- * @param flag
- * 是否查看日志
- * @version 1.2 li'mingqi
- */
- public void openLogInfo(boolean flag) {
- LPushUser.SAVELOG(getApplicationContext(), flag);
- }
- /***
- * socket链接重置,服务器一直处于拒绝链接状态,客户端链接一次遭拒后标记了遭拒的状态,重置之后可进行再次开启链接;
- *
- * @version 1.3 li'mingqi
- */
- private void reset() {
- LPushUser.saveConnect(getApplicationContext(), false);
- }
- }