From 82954841147ed6e258fefcff926697885ef65d7b Mon Sep 17 00:00:00 2001 From: endian11 <Dana_Lee1016@126.com> Date: 星期五, 13 十二月 2019 14:28:11 +0800 Subject: [PATCH] 目前可以走通连接,和发送数据;需要关注ByteBuf是否需要手动释放,什么时候释放; 修改了google json array解析数据 --- im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java | 14 +++++++++++--- 1 files changed, 11 insertions(+), 3 deletions(-) diff --git a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java index 3867e0d..4306b86 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java +++ b/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("瑙f瀽鍦板潃[%s],port[%d]",currentHost,currentPort)); //杩炴帴鏈嶅姟鍣� toServer(); //channel涓嶄负绌猴紝鍒欒涓鸿繛鎺ュ凡缁忔垚鍔� -- Gitblit v1.8.0