void Initialize() { state.Shots = false; countFramesBallStopped = 0; bufferBalls = new List <GameObject>(); dtoClientState = new DTOClientState(); // WebSocket で Server に接続 if (GameControllerState.IsOnline) { ConnectToServer(); } }
void ConnectToServer() { ws_connection = new WebSocket($"ws://{Constants.serverHost}:{Constants.serverPort}/ws"); // ハンドラ登録 ws_connection.OnOpen += (sender, e) => { Debug.Log("ws.OnOpen()"); }; ws_connection.OnMessage += (sender, e) => { Debug.Log($"ws.OnMessage(): {e.Data}"); // 初回 if (string.Compare(Constants.srvm_activeTurn, e.Data) == 0) { if (state.IsWatingTurn) { state.Change(); } else { state.ErrorState(); } } else if (string.Compare(Constants.srvm_notActiveTurn, e.Data) == 0) { // nop } else { // 初回以降 dtoClientState = JsonUtility.FromJson <DTOClientState>(e.Data); //Debug.Log($"dto: {dto}"); if (dtoClientState == null) { state.ErrorWebSocket(); } else { // このコンテキストで GameObject の transform を変更するとエラー発生 -> Update() で実行させる actionTask = () => { Debug.Log("actionTask()/Sync"); // ボール位置の同期 if (!SyncBallPositions(dtoClientState.BallStates)) { state.ErrorWebSocket(); return; } // ゲームオーバー判定 if (dtoClientState.IsGameOver) { state.ChangeToGameOver(); return; } if (dtoClientState.IsActiveTurn) { if (state.IsWatingTurn) { state.Change(); } else { state.ErrorState(); } } else { // nop } }; } } }; ws_connection.OnClose += (sender, e) => { Debug.Log("ws.OnClose()"); state.ErrorWebSocket(); ws_connection = null; }; ws_connection.Connect(); }