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;
|
|
@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();
|
}
|
}
|
|
}
|
}
|
*/
|
}
|