소스 검색

<tcp优化,更新铃声等>

weizhengliang 4 년 전
부모
커밋
454db99c9f

+ 2 - 1
middleware/src/main/code/com/wdkl/ncs/android/middleware/common/Constants.kt

@@ -40,7 +40,8 @@ class Constants {
         //刷新呼叫记录
         val EVENT_REFRESH_CALL_LIST = 0x04
 
-        val EVENT_SPEECH = 0x05
+        //新的来电
+        val EVENT_NEW_CALL = 0x05
 
         //手柄拿起
         val HOOK_OFF = "com.android.PhoneWinowManager.HOOK_OFF"

+ 19 - 12
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/TcpClient.java

@@ -8,7 +8,11 @@ import java.util.Scanner;
 import java.util.concurrent.TimeUnit;
 
 import io.netty.bootstrap.Bootstrap;
-import io.netty.channel.*;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioSocketChannel;
@@ -23,14 +27,16 @@ import io.netty.util.CharsetUtil;
 public class TcpClient {
     private String TAG = TcpClient.class.getSimpleName();
 
-    private NioEventLoopGroup workGroup = new NioEventLoopGroup();
+    private NioEventLoopGroup workGroup = new NioEventLoopGroup(2);
     public Channel channel;
     private Bootstrap bootstrap;
 
     //数据处理
-    TcpClientHandler tcpClientHandler = new TcpClientHandler();
+    private TcpClientHandler tcpClientHandler = new TcpClientHandler();
+    //是否运行中
+    public boolean isRunning = false;
     //重试间隔
-    private Integer retrySeconds = 2;
+    private Integer retrySeconds = 5;
     //重试计数
     private Integer retryTimes = 1;
 
@@ -61,14 +67,14 @@ public class TcpClient {
                         // LengthFieldPrepender是一个编码器,主要是在响应字节数据前面添加字节长度字段
                         socketChannel.pipeline().addLast(new LengthFieldPrepender(2));
                         //心跳包应当小于服务器间隔
-//                        socketChannel.pipeline().addLast(new IdleStateHandler(0, hbSeconds, 0, TimeUnit.SECONDS));
-                        socketChannel.pipeline().addLast(new IdleStateHandler(hbSeconds+1, hbSeconds, 0, TimeUnit.SECONDS));
+                        socketChannel.pipeline().addLast(new IdleStateHandler(0, 0,hbSeconds, TimeUnit.SECONDS));
                         socketChannel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
                         socketChannel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
                         socketChannel.pipeline().addLast(tcpClientHandler);
                     }
                 }).remoteAddress(serverIP, serverPort);
         doConnect();
+        System.out.println("connect server host: " + serverIP + ", port: " + serverPort);
     }
 
     //独立连接方法,用于重新连接
@@ -78,23 +84,24 @@ public class TcpClient {
             return;
         }
 
-        System.out.println("TcpClient connect start");
+        System.out.println("connect start");
         ChannelFuture future = bootstrap.connect().addListener(new ChannelFutureListener() {
             @Override
             public void operationComplete(ChannelFuture channelFuture) throws Exception {
-                if (channelFuture.isSuccess()) {
-                    channel = channelFuture.channel();
+                if (channelFuture.isSuccess()){
+                    isRunning = true;
                     retryTimes = 0;
-                    System.out.println("TcpClient connect success");
+                    System.out.println("connect success");
                 } else {
                     //连接失败时的处理
+                    isRunning = false;
                     System.out.println("TcpClient connect retry : " + retryTimes);
                     channelFuture.channel().eventLoop().schedule(new Runnable() {
                         @Override
                         public void run() {
                             retryTimes++;
-                            if (retryTimes > 30) {
-                                System.out.println("TcpClient 重试" + (retryTimes - 1) + "次,结束");
+                            if (retryTimes>30){
+                                System.out.println("重试"+(retryTimes-1)+"次,结束");
                                 workGroup.shutdownGracefully();
                                 //todo: 从API获取新的serverIP和serverPort,全新连接
                                 TcpClient.getInstance().init(Constants.Companion.getTcp_ip(), Integer.parseInt(Constants.Companion.getTcp_port()), Integer.parseInt(Constants.Companion.getReader_idle_time()));

+ 20 - 13
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/TcpClientHandler.java

@@ -39,6 +39,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
         super.channelActive(ctx);
+        Log.i(TAG, "连接成功");
         this.ctx = ctx;
         connected = true;
         retryTimes = 0;
@@ -54,20 +55,22 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
         super.channelInactive(ctx);
         connected = false;
-        System.out.println("TcpClientHandler 失去连接");
-        reConnect(ctx);
 
         Constants.Companion.setTcp_connected(false);
         EventBus.getDefault().post(new MessageEvent(0, Constants.Companion.getEVENT_TCP_STATE()));
+		
+		Log.i(TAG, "失去连接");
+        TcpClient.getInstance().doConnect();
+        //reConnect(ctx);
     }
 
     //读取String消息
     @Override
     protected void channelRead0(ChannelHandlerContext ctx, String source) throws Exception {
-        System.out.println("TcpClientHandler from server ===> " + source);
-        if (source.equals("1")) {
-            Log.e(TAG, "收到服务器返回的心跳" + "source " + source);
-        } else {
+        //System.out.println("channelRead0: read callback from server ===> " + source);
+        if(source.equals("1")){
+            Log.i(TAG,"收到服务器返回的心跳 "+source);
+        }else {
             TcpModel tcpModel = TcpModel.getModelByJson(source);
 
             TcpModel responseTcpModel = DeviceChannel.handleTcpReceived(tcpModel);
@@ -82,14 +85,18 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
     //写心跳包。没有消息发送时,每间隔一定时间会由此方法向服务端发送心跳
     @Override
     public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
-        if (evt instanceof IdleStateEvent) {
-            IdleStateEvent event = (IdleStateEvent) evt;
-            if (event.state() == IdleState.WRITER_IDLE) {
-                ctx.writeAndFlush("0");
+        if (evt instanceof IdleStateEvent){
+            IdleStateEvent event = (IdleStateEvent)evt;
+            if (event.state()== IdleState.WRITER_IDLE){
                 //读心跳包超时执行
-            }else if (event.state() == IdleState.READER_IDLE) {
-                Log.i(TAG, "TcpClientHandler ===> pong from server failed");
-                ctx.close();
+                Log.i(TAG,"TcpClientHandler ===> WRITER_IDLE");
+            } else if (event.state() == IdleState.READER_IDLE){
+                //写心跳包超时
+                Log.i(TAG,"TcpClientHandler ===> READER_IDLE");
+            } else if (event.state() == IdleState.ALL_IDLE){
+                //写心跳包超时
+                Log.i(TAG,"TcpClientHandler ===> ALL_IDLE");
+                ctx.writeAndFlush("0");
             }
         }
     }

+ 4 - 4
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/channel/DeviceChannel.java

@@ -41,18 +41,18 @@ public class DeviceChannel {
                 if (tcpModel.getAction()== TcpAction.VoiceAction.CALL){ //语音呼入
                     InteractionVO interactionVO = new Gson().fromJson(tcpModel.getData().toString(), InteractionVO.class);
                     //有新的呼叫进来,添加到语音播报队列
-                    EventBus.getDefault().post(new MessageEvent(tcpModel, Constants.Companion.getEVENT_SPEECH()));
+                    EventBus.getDefault().post(new MessageEvent(tcpModel, Constants.Companion.getEVENT_NEW_CALL()));
                     //todo: 判断当前是否通话
                     // 通话中
                     if (calling){
-                        responseTcpModel = VoiceUtil.voiceCalling(tcpModel.getToId(), tcpModel.getFromId(),interactionVO.getId());
+                        responseTcpModel = VoiceUtil.voiceCalling(Integer.parseInt(Constants.Companion.getIds()), tcpModel.getFromId(),interactionVO.getId());
                         //todo 给服务器发送正在通话中 tcp
                         return responseTcpModel;
                     } else {
-                        responseTcpModel = VoiceUtil.voiceSuccess(tcpModel.getToId(), tcpModel.getFromId(),interactionVO.getId());
                         //todo: 通话中界面展现,data中服务器传过来呼入名称;从接口重新获取左侧数据
                         EventBus.getDefault().post(new MessageEvent(tcpModel, Constants.Companion.getEVENT_TCP_MSG()));
-                        return responseTcpModel;
+                        //responseTcpModel = VoiceUtil.voiceSuccess(Integer.parseInt(Constants.Companion.getIds()), tcpModel.getFromId(),interactionVO.getId());
+                        //return responseTcpModel;
                     }
                 } else if (tcpModel.getAction()== TcpAction.VoiceAction.ACCEPT){ //我方呼出,对方接受
                     //todo: 通话中界面更新;建立数据通话

+ 3 - 3
nursehome/src/main/java/com/wdkl/ncs/android/component/nursehome/activity/NurseHomeActivity.kt

@@ -642,7 +642,7 @@ fun call(tyte:Int){
     }
 
     fun initCountDownTimer() {
-        countDownTimer = object: CountDownTimer(60*1000L, 1000) {
+        countDownTimer = object: CountDownTimer(30*1000L, 1000) {
             override fun onTick(millisUntilFinished: Long) {
                 //
             }
@@ -711,7 +711,6 @@ fun call(tyte:Int){
                             CallDialogHelper.dismissCallDialog()
                         })
 
-                        EventBus.getDefault().post(MessageEvent(0, Constants.EVENT_REFRESH_CALL_LIST))
                     } else if (tcpModel.getAction() == TcpAction.VoiceAction.ACCEPT) {//对方接受语音
                         Log.e(TAG, "对方接受语音" + tcpModel.toJson())
                         Constants.interactionId = interactionVO.id
@@ -776,7 +775,7 @@ fun call(tyte:Int){
                 }
             }
 
-            Constants.EVENT_SPEECH -> {
+            Constants.EVENT_NEW_CALL -> {
                 if (Constants.CALL_STATE != Constants.CALL_CALLING) {
                     if (SettingConfig.getTtsMode(this) == SettingConfig.TTS_ON) {
                         val tcpModel = messageEvent.getMessage() as TcpModel
@@ -785,6 +784,7 @@ fun call(tyte:Int){
                         SpeechUtil.getInstance().addSpeech(frameName + "呼叫", false)
                     }
                 }
+                EventBus.getDefault().post(MessageEvent(0, Constants.EVENT_REFRESH_CALL_LIST))
             }
 
             //TCP连接状态

BIN
webrtc/src/main/res/raw/incoming_call.mp3