im_lib/src/main/java/com/anyun/im_lib/ExecutorServiceFactory.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
im_lib/src/main/java/com/anyun/im_lib/LoginAuthRespHandler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
im_lib/src/main/java/com/anyun/im_lib/netty/TCPReadHandler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
im_lib/src/main/java/com/anyun/im_lib/ExecutorServiceFactory.java
@@ -1,5 +1,7 @@ package com.anyun.im_lib; import android.util.Log; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -10,6 +12,8 @@ * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class ExecutorServiceFactory { private static final String TAG = ExecutorServiceFactory.class.getSimpleName(); /*** 管理线程组,负责重连***/ private ExecutorService bossPool; @@ -38,6 +42,7 @@ if (bossPool == null){ initBossLoopGroup(); } Log.i(TAG, "execBossTask"); bossPool.execute(runnable); } /** im_lib/src/main/java/com/anyun/im_lib/LoginAuthRespHandler.java
@@ -36,7 +36,7 @@ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); Log.i(TAG, "channelRead"+(String)msg); // Log.i(TAG, "channelRead"+(String)msg); // TODO: 2019/12/4 } } im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java
@@ -12,10 +12,13 @@ import com.anyun.im_lib.listener.IMSConnectStatusCallback; import com.anyun.im_lib.listener.OnEventListener; import java.nio.ByteBuffer; import java.util.Vector; import java.util.function.ToDoubleBiFunction; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import io.netty.channel.Channel; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; @@ -176,13 +179,14 @@ try { channel = bootstrap.connect(currentHost,currentPort).sync().channel(); } catch (InterruptedException e) { Log.i(TAG, String.format("连接Server(ip[%s],port[%d]失败)",currentHost,currentPort)); try { Thread.sleep(500); } catch (InterruptedException ex) { ex.printStackTrace(); } Log.i(TAG, String.format("连接Server(ip[%s],port[%d]失败)",currentHost,currentPort)); channel = null; } } @@ -318,7 +322,9 @@ Log.i(TAG, "sendMsg fail,channel为空"+msg); } try { channel.writeAndFlush(msg); ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(); byteBuf.writeBytes(msg.getBytes()); channel.writeAndFlush(byteBuf); } catch (Exception e) { Log.i(TAG, "发送消息失败,reason="+e.getMessage()+"\t"+msg); } @@ -388,6 +394,7 @@ @Override public void run() { Log.i(TAG, "执行重连任务"); if (!isFirst){ onConnectStatusCallback(IMSConfig.CONNECT_STATE_FAILURE); } @@ -458,7 +465,7 @@ return IMSConfig.CONNECT_STATE_FAILURE; } String[] address = serverUrl.split(" "); for (int j = 0; j < IMSConfig.DEFAULT_RECONNECT_COUNT; j++) { for (int j = 1; j < IMSConfig.DEFAULT_RECONNECT_COUNT; j++) { //如果ims已经关闭,或网络不可用,直接回调连接状态,不再进行连接 if (isClosed || !isNetworkAvaliable()){ return IMSConfig.CONNECT_STATE_FAILURE; @@ -471,6 +478,7 @@ try { currentPort = Integer.parseInt(address[1]); currentHost = address[0]; Log.i(TAG, String.format("解析地址[%s],port[%d]",currentHost,currentPort)); //连接服务器 toServer(); //channel不为空,则认为连接已经成功 im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java
@@ -50,13 +50,13 @@ // 为了更加清楚的说明一下上面的规则,调整一下例子中的代码。在写入通道前,在数据 // pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,4,0,13)); pipeline.addLast(new FixedLengthFrameDecoder(31)); // 测试用 固定长度消息 pipeline.addLast(new FixedLengthFrameDecoder(10)); // 测试用 固定长度消息 //握手认证消息相应处理handler pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient)); // pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient)); //心跳消息响应处理handler pipeline.addLast(HeartbeatRespHandler.class.getSimpleName(), new HeartbeatRespHandler(imsClient)); // pipeline.addLast(HeartbeatRespHandler.class.getSimpleName(), new HeartbeatRespHandler(imsClient)); //接收消息处理handler pipeline.addLast(TCPReadHandler.class.getSimpleName(),new TCPReadHandler(imsClient)); im_lib/src/main/java/com/anyun/im_lib/netty/TCPReadHandler.java
@@ -3,7 +3,12 @@ import android.util.Log; import com.anyun.im_lib.interf.IMSClientInteface; import com.anyun.im_lib.util.ByteUtil; import java.nio.ByteBuffer; import java.nio.charset.Charset; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; @@ -45,6 +50,7 @@ Channel channel = ctx.channel(); if (channel != null){ channel.close(); ctx.close(); } //触发重连 imsClient.resetConnect(false); @@ -54,13 +60,21 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); // TODO: 2019/12/4 Log.i(TAG, "channelRead: "); imsClient.getMsgDispatcher().receivedMsg((String)msg); //服务端返回消息后 ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String str = new String(req, "UTF-8"); System.out.println("服务端数据为 :" + str); Log.i(TAG, "channelRead: "+ str); imsClient.getMsgDispatcher().receivedMsg( str ); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { super.channelReadComplete(ctx); Log.i(TAG, "channelReadComplete"); } } im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java
New file @@ -0,0 +1,39 @@ package com.anyun.im_lib.util; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; /** * MyApplication2 * Created by lzw on 2019/12/13. 12:01:18 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class ByteUtil { /** * ByteBuffer 转换 String * @param buffer * @return */ public static String getString(ByteBuffer buffer) { Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer);//用这个的话,只能输出来一次结果,第二次显示为空 // charBuffer = decoder.decode(buffer.asReadOnlyBuffer()); return charBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); return ""; } } }