Skip to content

已经和服务端建立了连接,字节数并不限制于126,目前可以发送三四万字符,再多就没测试了,可以自己修改自己数,心跳已经保活也已经写好,新手很方便阅读,log也已经加上了

Notifications You must be signed in to change notification settings

byron-xie/android-socket-Demo

Repository files navigation

androidWebsocketDemo

android使用websocket进行长链接的一个简单的demo,该项目适合嵌入到系统,也可以增加监听保活和心跳代码已经写好,适合小白阅读和理解

目前可以容纳10万字节 三四万中文字符不成问题,如果自己想要更大可以修改,这个demo可以接收报文,如果文件巨大建议用http上传下载

##基本操作都在WebSocketService 这个类中,websocketHost要填写自己服务器的,ws开头的url; 服务端的url为测试,可以顺利收发消息

    if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        boolean isDisConect = networkInfo == null || !networkInfo.isAvailable();
        if (isDisConect) {
            Toast.makeText(context, "网络已断开,请重新连接", Toast.LENGTH_SHORT).show();
                Log.e("HeartReceiver","网络已断开,取消定时");
              context.sendBroadcast(new Intent(ACTION_STOP_HEART));
            }else {
            context.startService(new Intent(context, WebSocketService.class));
            context.sendBroadcast(new Intent(ACTION_START_HEART));
        }
            }
    if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
        context.startService(new Intent(context, WebSocketService.class));
    }

       心跳,2个任务 保持长连接,并检测连接是否正常,不正常或者断网(断网分3种,一种断网未断开,一种已经网和连接断开,另外一种 网未断,连接段)

                     if (Const.ACTION_START_HEART.equals(action)) {

             //启动心跳

               if(webSocketConnection==null||!webSocketConnection.isConnected()){

                Log.e("HeartReceiver","onReceive webSocketConnect initPendingIntent");

                   //初次心跳检测连接是否存在,不存在或未连接

                   if (webSocketConnection != null) {

                    try {

                           webSocketConnection.disconnect();//如果存在连接对象 连接不上 则取消连接

                       }catch (IllegalStateException e){

                    }
                    
                }

                   webSocketConnect();//初始化连接 并连接

               }

               initPendingIntent();//初始化心跳

        } else if (ACTION_HEARTBEAT.equals(action)) {
        
            //在此完成心跳需要完成的工作,比如请求远程服务器……
            
            ConnectivityManager systemService = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
            
            NetworkInfo networkInfo = systemService.getActiveNetworkInfo();

               if (networkInfo == null || !networkInfo.isAvailable()) {//检测网络

               //第一种情况

               } else {

              if (webSocketConnection==null||!webSocketConnection.isConnected()) 

                       if (webSocketConnection != null) { //第二种情况

                        webSocketConnection.disconnect();
                        
                    }
                    
                    webSocketConnect();
                    
                }else {
                
                    context.sendBroadcast(messageIntent);
                    
                    webSocketConnection.sendTextMessage("心跳开始___心跳次数为:"+count++);
                    
                }
                

            }
            
        } else if (Const.ACTION_STOP_HEART.equals(action)) {

           //停止心跳 ,节省电量

               Log.d(TAG, "Stop heart");

            try{
            
                AlarmManager    mAlarmManager = (AlarmManager)applicationContext. getSystemService(ALARM_SERVICE);
                
                mAlarmManager.cancel(mPendingIntent);
                

            }catch (Exception e){}
            
            mPendingIntent=null;
            

        }
        
    }

``

About

已经和服务端建立了连接,字节数并不限制于126,目前可以发送三四万字符,再多就没测试了,可以自己修改自己数,心跳已经保活也已经写好,新手很方便阅读,log也已经加上了

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages