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;

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章