| | |
| | | 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; |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | 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); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public void run() { |
| | | Log.i(TAG, "执行重连任务"); |
| | | if (!isFirst){ |
| | | onConnectStatusCallback(IMSConfig.CONNECT_STATE_FAILURE); |
| | | } |
| | |
| | | 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; |
| | |
| | | try { |
| | | currentPort = Integer.parseInt(address[1]); |
| | | currentHost = address[0]; |
| | | Log.i(TAG, String.format("解析地址[%s],port[%d]",currentHost,currentPort)); |
| | | //连接服务器 |
| | | toServer(); |
| | | //channel不为空,则认为连接已经成功 |