WeChatアップルは、非表示になるとWebソケット接続を切断します

まず、アップルトがWebソケットに接続し、ユーザーopenidをサーバーに送信します


    wx.connectSocket({
      url: "ws://192.168.1.104:9090/xcxmvc/so",
   
    })

    //连接成功
    wx.onSocketOpen(function () {
      console.log("c" + that.data.openid);
      wx.sendSocketMessage({
        data: that.data.openid,
      })
    })
    wx.onSocketMessage(function (res) {
      var objData = JSON.parse(res.data);
      console.log(objData);
      that.setData({ nr: objData });
    })


    //连接失败
    wx.onSocketError(function () {
      console.log('websocket连接失败!');
    })

サーバーはopenidを受信すると、タイマーを開始して、ユーザーが応答を受信したかどうかを確認します。応答があり、セッションが閉じられていないかどうかを
毎秒確認し
ます。

メッセージをクライアントにプッシュします。
判断する必要があります
。session.isopenがtrueの場合は、メッセージをクライアントに送信します
。それ以外の場合は、タイマーを停止します。


		        if(s.isOpen()){
		       
			try {
				s.getBasicRemote().sendText(jsonObject);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.print("fa song cuo le");
			}
		        }
		        else{
		        	System.out.print("fa song cuo le");
		        	idd=1;
		        	cancel();
		        
		        }
@OnMessage
public void onmessage(final String data,final Session s){
	System.out.println(data+s.getId());
	 
	final String shou="shoudao";
	 String panduan="s";
	TimerTask task = new TimerTask() { //创建一个新的timer task 
		public void run() { //定时器任务执行的操作
			Gson gson = new Gson();
			 dao dd= new dao();
			 HashMap<String,List> aa=new HashMap<String,List>();
			 
			// List aa=null;
			// nrbean n=new nrbean();
			// List chakan=new ArrayList();
			 List<nrbean> li=new ArrayList<nrbean>();// po=null;
		        li=dd.chakan(data);
		      String jsonObject="";
		      ///通过下面方法能取出list中想要的内容
		       // for(int i=0;i<li.size();i++){   
		       // int id;
		        //	nrbean nb=li.get(i);
			   //  id=nb.getChakancichu();
			    // if(id==1){}
			      jsonObject = gson.toJson(li);
			        System.out.print("------------"+s.isOpen());
			     //   }
		    
		        if(s.isOpen()){
		       
			try {
				s.getBasicRemote().sendText(jsonObject);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.print("fa song cuo le");
			}
		        }
		        else{
		        	System.out.print("fa song cuo le");
		        	idd=1;
		        	cancel();
		        	try {
						s.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		        }
			System.out.println("现在是:" +data+"..."+s.getId());
		       
			
		}};
	
	
	Timer timer = new Timer();//创建一个定时器
	long delay = 0;
	long PeriodTime = 1 * 1000;
	
	//重复执行特定任务,第一个参数为要执行的任务,第二个为执行任务之前延迟的时间,第三个为时间间隔
	//单位都是毫秒
	//timer.
	
		 timer.scheduleAtFixedRate(task, delay, PeriodTime);
	 
}


}

アップルトページがオンになったらソケットを閉じますHide

 onHide:function(){ 
    wx.closeSocket({

    })// {
  },

このとき、サーバー側が実行されます

@OnClose
public void end() {
	System.out.print("走了");
}

おすすめ

転載: blog.csdn.net/weixin_40938312/article/details/105091942