From d6448fe2e03fd5adb5e32e5b7ff79af3cd70f72e Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 5 Aug 2018 23:38:11 +0900 Subject: [PATCH] :v: --- src/front.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/front.ts b/src/front.ts index 085bbea..fadf7f0 100644 --- a/src/front.ts +++ b/src/front.ts @@ -26,6 +26,8 @@ homeStream.addEventListener('open', () => { homeStream.addEventListener('close', () => { console.log('home stream closed'); + + process.exit(1); }); homeStream.addEventListener('message', message => { @@ -127,6 +129,14 @@ function gameStart(game) { // ゲームストリームに接続 const gw = new WebSocket(`${wsUrl}/games/reversi-game?i=${config.i}&game=${game.id}`); + function send(msg) { + try { + gw.send(JSON.stringify(msg)); + } catch (e) { + console.error(e); + } + } + gw.addEventListener('open', () => { console.log('reversi game stream opened'); @@ -171,10 +181,10 @@ function gameStart(game) { ai.on('message', msg => { if (msg.type == 'put') { - gw.send(JSON.stringify({ + send({ type: 'set', pos: msg.pos - })); + }); } else if (msg.type == 'close') { gw.close(); } @@ -189,17 +199,17 @@ function gameStart(game) { // フォーム初期化 setTimeout(() => { - gw.send(JSON.stringify({ + send({ type: 'init-form', body: form - })); + }); }, 1000); // どんな設定内容の対局でも受け入れる setTimeout(() => { - gw.send(JSON.stringify({ + send({ type: 'accept' - })); + }); }, 2000); });