websocket重新配置发送指定命令--5时间计时器

本次将连接人数、发送还是连接的状态、页面信息处理加以完善

如果您这边分开调用理解的不是太好可以借鉴我之前发布的websocket专题1~5参考查阅(忘记了是否都被发布)

1.Timer java文件实现runnable接口

import java.util.HashMap;import java.util.Map;import javax.websocket.OnMessage;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;import org.apache.log4j.Logger;import org.sceu.tkdtas.Timer;import org.sceu.tkdtas.constants.Constants;import org.springframework.util.StringUtils;import com.google.gson.Gson;/** * 计时器 * @author sunyan * */@ServerEndpoint("/timer")public class WSTimer extends WSBase {Logger log = Logger.getLogger(WSTimer.class);Gson gson = new Gson();@Override@OnMessage public void onMessage(String msg, Session session) {log.info("【REQ_MSG】:" + msg);@SuppressWarnings("unchecked")Map reqJsonMap = gson.fromJson(msg, HashMap.class);

String requestType = (String)reqJsonMap.get(Constants.WS_REQPARA_REQUESTTYPE);

if(StringUtils.isEmpty(requestType)) {

return;

}

boolean timerControler = false;

if(Constants.WS_REQTYPE_START.equals(requestType)) {

timerControler = true;

}else if(Constants.WS_REQTYPE_PAUSE.equals(requestType)) {

timerControler = true;

}else if(Constants.WS_REQTYPE_START_PAUSE_STOP.equals(requestType)) {

timerControler = true;

}else if(Constants.WS_REQTYPE_RESET_tIME.equals(requestType)) {

timerControler = true;

}

if(!timerControler) {

log.info("非时间操作!");

return;

}

Timer timer = new Timer(reqJsonMap);

Thread thread = new Thread(timer);

thread.start();

log.info("【" + Thread.currentThread().getName()+"】 开始执行");

super.onMessage(msg, session);

}

}


2.Timer  java文件 具体实现每秒向页面发送时间

import java.util.HashMap;import java.util.Map;import org.sceu.tkdtas.ws.WSBase;import org.sceu.tkdtas.ws.WebSocketPool;import org.apache.log4j.Logger;import org.sceu.tkdtas.constants.Constants;import com.google.gson.Gson;/** * 计时器 * @author sunyan * */public class Timer implements Runnable {Logger log = Logger.getLogger(Timer.class);Gson gson = new Gson();/** * 当前线程名称 */String threadName;private MapreqMsgMap;public Timer(MapreqMsgMap) {this.reqMsgMap = reqMsgMap;}public void run() {threadName = Thread.currentThread().getName();/** * 开始暂停标识 * 1:开始; * 2:暂停; */String requestType = (String) reqMsgMap.get("requestType"); if(Constants.WS_REQTYPE_START.equals(requestType)) {log.info("【"+threadName+"】 比赛时间开始计时");Constants.FLAG_MATCH_START = true;Constants.FLAG_PAUSE_START = false;matchTimeStart();}else if(Constants.WS_REQTYPE_PAUSE.equals(requestType)) { log.info("【"+threadName+"】 暂停时间开始计时");Constants.FLAG_MATCH_START = false;Constants.FLAG_PAUSE_START = true;pauseTimeStart();}else if(Constants.WS_REQTYPE_START_PAUSE_STOP.equals(requestType)) {log.info("【"+threadName+"】 暂停时间开始计时--时间停止操作");Constants.FLAG_MATCH_START = false;Constants.FLAG_PAUSE_START = false;matchTimeStart();pauseTimeStart();}else if(Constants.WS_REQTYPE_RESET_tIME.equals(requestType)) {log.info("【"+threadName+"】 暂停时间开始计时--重置时间操作");resetTime();}}private void resetTime() {Constants.CACHE_MATCH_TIME = 0;Constants.CACHE_PAUSE_TIME = 0;/** * 比赛时间 *///long matchTime = ((Double)reqMsgMap.get("matchTime")).longValue();String matchTime = (String)reqMsgMap.get("matchTime");/** * 暂停时间 *///long pauseTime = ((Double)reqMsgMap.get("pauseTime")).longValue();String pauseTime = (String)reqMsgMap.get("pauseTime");for(int i=0;i<1;i++) {MapjsonMap = new HashMap();jsonMap.put("matchTime", matchTime); jsonMap.put("pauseTime", pauseTime);String resJson = gson.toJson(jsonMap);for(WSBase item : WebSocketPool.wsSet){try {item.sendMessage(resJson);} catch (Exception e) {e.printStackTrace();continue;}}jsonMap = null;}}private void pauseTimeStart() {while(Constants.FLAG_MATCH_START_RUNING) {log.debug("当前比赛时间正在执行,等待.....");}Constants.FLAG_MATCH_START_RUNING = true;/** * 比赛时间 */long matchTime = Constants.CACHE_MATCH_TIME;/** * 暂停时间 */long pauseTime = ((Double)reqMsgMap.get("pauseTime")).longValue();//System.out.println("====== strPauseTime :"+strPauseTime+" ===========");//long pauseTime = new Double((String)reqMsgMap.get("pauseTime")).longValue(); while(pauseTime > 0) {if(!Constants.FLAG_PAUSE_START) {//Constants.CACHE_PAUSE_TIME = pauseTime; return;}pauseTime -= 1000;try {MapjsonMap = new HashMap();jsonMap.put("matchTime", matchTime); jsonMap.put("pauseTime", pauseTime);String resJson = gson.toJson(jsonMap);for(WSBase item : WebSocketPool.wsSet){try {item.sendMessage(resJson);} catch (Exception e) {e.printStackTrace();continue;}}jsonMap = null;Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}private void matchTimeStart() {/** * 比赛时间 */long matchTime = Constants.CACHE_MATCH_TIME;if(matchTime == 0) {matchTime = getLong(reqMsgMap.get("matchTime"));}/** * 暂停时间 */String strPauseTime = (String)reqMsgMap.get("pauseTime");log.info("strPauseTime ::::: " + strPauseTime);long pauseTime = Long.parseLong(strPauseTime);/** * 比赛时间 */long matichTimeMillisecond = matchTime;try {Thread.sleep(1000);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}while(matichTimeMillisecond > 0) {if(!Constants.FLAG_MATCH_START) {Constants.CACHE_MATCH_TIME = matichTimeMillisecond;Constants.FLAG_MATCH_START_RUNING = false;return;}matichTimeMillisecond -= 1000;try {MapjsonMap = new HashMap();

jsonMap.put("matchTime", matichTimeMillisecond);

jsonMap.put("pauseTime", pauseTime);

String resJson = gson.toJson(jsonMap);

for(WSBase item : WebSocketPool.wsSet){

try {

item.sendMessage(resJson);

} catch (Exception e) {

e.printStackTrace();

continue;

}

}

jsonMap = null;

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

log.info("将FLAG_MATCH_START_RUNING置为false"); 

Constants.CACHE_MATCH_TIME = matichTimeMillisecond;

Constants.FLAG_MATCH_START_RUNING = false;

}

private long getLong(Object object) {

if(object == null)

return 0;

long rtnLong = 0L;

if(object instanceof Double ) {

rtnLong = ((Double)object).longValue();

}else if(object instanceof String) {

String strLong = (String)object;

log.info("strLong : " + strLong);

if(strLong.indexOf(".") == -1) {

rtnLong = new Long(strLong).longValue();

}else {

rtnLong = new Double(strLong).longValue();

}

}

return rtnLong;

}

}

你可能感兴趣的:(websocket重新配置发送指定命令--5时间计时器)