package com.ntlab.irisserver.websockets; import com.ntlab.irisserver.entities.Game; import com.ntlab.irisserver.entities.Room; import com.ntlab.irisserver.models.KeywordManager; import com.ntlab.irisserver.models.RoomManager; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @ServerEndpoint("/game-start") public class GameStartWebSocket implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>(); static { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); } @OnMessage public String onMessage(String message) throws IOException { // rid,nickname,2 でメッセージが来るはず System.out.println("WebSocketで受信したメッセージ/ " + message); String[] msgs = message.split(","); String rid = msgs[0]; String nickname = msgs[1]; Integer state = Integer.parseInt(msgs[2]); System.out.println(rid); System.out.println(nickname); System.out.println(state); RoomManager rm = RoomManager.getInstance(); Room room = rm.getRoom(rid); if(nickname.equals(room.ownerName)){ //nicknameがownerだった場合 room.setState(state); //部屋の情報を変更 if (state == 2) { String path; path = applicationContext.getResource("file:").getFile().getAbsolutePath()+"/apache-tomcat-9.0.10/webapps/irisdata/keywords.txt"; KeywordManager km = KeywordManager.getInstance(); String[] keywords = km.getKeywords(path); Game game = new Game(room, keywords); } } return "WebSocketでメッセージを正常に受信しました: " + message; } @OnError public void onError(Throwable th) { System.out.println("WebSocketエラーが発生/ " + th.getMessage()); } @OnOpen public void onOpen(Session session) { System.out.println("WebSocketセッション確立"); sessions.add(session); } @OnClose public void onClose(Session session) { System.out.println("WebSocketセッション終了"); sessions.remove(session); } }