lxw
2020-07-11 9db52f2f2dd3665fe9da1ae5657e0167c3a34d40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.fgkj.websockets;
 
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArraySet;
 
import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
 
import com.fgkj.actions.ActionUtil;
import com.fgkj.dto.ServiceModel;
import com.fgkj.dto.User_inf;
import com.fgkj.dto.ram.Fbs9100_state;
import com.fgkj.services.ram.Fbs9100_stateService;
 
@ServerEndpoint("/ws/Fbs9100_stateSocket")
public class Fbs9100_stateSocket extends ActionUtil{
    private Fbs9100_stateService service = new Fbs9100_stateService();
    
    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    static int onlineCount = 0;
 
    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
    private static CopyOnWriteArraySet<Fbs9100_stateSocket> webSocketSet = new CopyOnWriteArraySet<Fbs9100_stateSocket>();
 
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session session;
    // 引入计时器
    private Timer timer = new Timer();
    private MyTask task= new MyTask(this, "1");
    
    private  HttpSession httpSession;
 
    /**
     * 连接建立成功调用的方法
     * @param session  可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    @OnOpen
    public void onOpen(Session session, EndpointConfig config){
        this.session = session;
        webSocketSet.add(this);     //加入set中
        addOnlineCount();           //在线数加1
        timer.schedule(task, 0, 4000);    // 开启计时器
        //System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
    }
 
    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(){
        timer.cancel();
        webSocketSet.remove(this);  //从set中删除
        subOnlineCount();           //在线数减1
        //System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
    }
   //11.1FBS9100设备通信状态查询
      public ServiceModel serchByCondition(String obj,int uid){
          Fbs9100_state state = getGson("yyyy-MM-dd HH:mm:ss").fromJson(obj, Fbs9100_state.class);
          //User_inf uinf=(User_inf) ActionUtil.getUser();
          state.setNum(uid);//将用户id存放在Fbs9100_state的num中用于处理用户管理的机房问题
          ServiceModel model = service.serchByCondition(state);
          return model;
      }
    /**
     * 收到客户端消息后调用的方法
     * @param message 客户端发送过来的消息
     * @param session 可选的参数
     */
    @OnMessage
    public void onMessage(String message,Session session) {
        task.setMessage(message);
        task.run();
    }
    /**
     * 发生错误时调用
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error){
        System.out.println("发生错误");
        error.printStackTrace();
    }
 
    /**
     * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException{
        this.session.getBasicRemote().sendText(message);
        //this.session.getAsyncRemote().sendText(message);
    }
 
    public static synchronized int getOnlineCount() {
        return WebSocketTest.onlineCount;
    }
 
    public static synchronized void addOnlineCount() {
        WebSocketTest.onlineCount++;
    }
 
    public static synchronized void subOnlineCount() {
        WebSocketTest.onlineCount--;
    }
 
    
    static class MyTask extends TimerTask {  
        private Fbs9100_stateSocket socket;
        private String message;
        private String result;
        public MyTask(Fbs9100_stateSocket socket, String message) {
            super();
            this.socket = socket;
            this.message = message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
        public String getResult() {
            return result;
        }
        
        @Override  
        public void run() {  
            if(!message.equals("1")){
                WebSocket ws=getGson().fromJson(message,  WebSocket.class);
                if(ws.getMethod()!=null){
                    if(ws.getMethod().equals("serchByCondition")){
                        String json=tojson(ws.getJson());
                        result=tojson(socket.serchByCondition(json,ws.getUid()));
                        //System.out.println("json:"+json);
                    }
                }else{
                        result="暂无结果!";
                }
                try {
                    if(result != "") {
                        socket.sendMessage(this.result);
                    }
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
            
       }
    } 
 
}