app层发送失败打印log; NettyTcp 设置连接超时为10s.原来是0默认30s连接超时
| | |
| | | CThreadPoolExecutor.runInBackground(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | if (IMSClientBootstrap.getInstance().isActive()){ |
| | | |
| | | IMSClientBootstrap.getInstance().sendMessage(msg); |
| | | }else{ |
| | | Log.e(TAG, "run: 发送消息失败,未初始化连接NettyTcp"); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | |
| | | private void toServer() { |
| | | try { |
| | | channel = bootstrap.connect(currentHost,currentPort).sync().channel(); |
| | | } catch (InterruptedException e) { |
| | | } catch (Exception e) { |
| | | Log.i(TAG, String.format("连接Server(ip[%s],port[%d]失败)",currentHost,currentPort)); |
| | | try { |
| | | Thread.sleep(500); |
| | |
| | | |
| | | @Override |
| | | public int getConnectTimeout() { |
| | | return 0; |
| | | if(mOnEventListener != null && mOnEventListener.getConnectTimeout()>0){ |
| | | connectTimeOut = mOnEventListener.getConnectTimeout(); |
| | | } |
| | | return connectTimeOut; |
| | | } |
| | | |
| | | @Override |
| | |
| | | return IMSConfig.CONNECT_STATE_FAILURE; |
| | | } |
| | | String[] address = serverUrl.split(" "); |
| | | for (int j = 1; 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; |
| | |
| | | // 在上面的例子中,自定义长度解码器的构造参数值如下: |
| | | // |
| | | // LengthFieldBasedFrameDecoder spliter=new LengthFieldBasedFrameDecoder(1024,0,4,0,4); |
| | | // 第一个参数为1024,表示数据包的最大长度为1024;第二个参数0,表示长度域的偏移量为0,也就是长度域放在了最前面,处于包的起始位置;第三个参数为4,表示长度域占用4个字节;第四个参数为0,表示长度域保存的值,仅仅为有效数据长度,不包含其他域(如长度域)的长度;第五个参数为4,表示最终的取到的目标数据包,抛弃最前面的4个字节数据,长度域的值被抛弃。 |
| | | // 第一个参数为1024,表示数据包的最大长度为1024; |
| | | // 第二个参数0,表示长度域的偏移量为0,也就是长度域放在了最前面,处于包的起始位置; |
| | | // 第三个参数为4,表示长度域占用4个字节; |
| | | // 第四个参数为0,表示长度域保存的值,仅仅为有效数据长度,不包含其他域(如长度域)的长度; |
| | | // 第五个参数为4,表示最终的取到的目标数据包,抛弃最前面的4个字节数据,长度域的值被抛弃。 |
| | | // |
| | | // 为了更加清楚的说明一下上面的规则,调整一下例子中的代码。在写入通道前,在数据 |
| | | // pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,4,0,13)); |
| | | pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,2,1,0)); |
| | | |
| | | pipeline.addLast(new FixedLengthFrameDecoder(10)); // 测试用 固定长度消息 |
| | | // pipeline.addLast(new FixedLengthFrameDecoder(10)); // 测试用 固定长度消息 |
| | | |
| | | // pipeline.addLast(new LineBasedFrameDecoder(1024)); |
| | | |
| | | //握手认证消息相应处理handler |
| | | pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient)); |
| | |
| | | import android.util.Log; |
| | | |
| | | import com.anyun.im_lib.interf.IMSClientInteface; |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | |
| | | import io.netty.buffer.ByteBuf; |
| | |
| | | ByteBuf buf = (ByteBuf) msg; |
| | | byte[] req = new byte[buf.readableBytes()]; |
| | | buf.readBytes(req); |
| | | Log.i(TAG, "channelRead hex str: "+ ByteUtil.byte2HexStr(req)); |
| | | String str = new String(req, "UTF-8"); |
| | | |
| | | Log.i(TAG, "channelRead: "+ str); |
| | | imsClient.getMsgDispatcher().receivedMsg( str ); |
| | | } |
| | | |
| | |
| | | { |
| | | stmp = Integer.toHexString(b[n] & 0xFF); |
| | | sb.append((stmp.length()==1)? "0"+stmp : stmp); |
| | | // sb.append(" "); |
| | | sb.append(" "); |
| | | } |
| | | return sb.toString().toUpperCase().trim(); |
| | | } |