From bd79352e8005bd8fe0ffa846e3318bc49b3fdd36 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 11 Mar 2018 18:08:26 +0900 Subject: #1204 --- src/api/stream/othello.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/api/stream') diff --git a/src/api/stream/othello.ts b/src/api/stream/othello.ts index 5056eb535c..bd3b4a7637 100644 --- a/src/api/stream/othello.ts +++ b/src/api/stream/othello.ts @@ -1,5 +1,8 @@ +import * as mongo from 'mongodb'; import * as websocket from 'websocket'; import * as redis from 'redis'; +import Matching, { pack } from '../models/othello-matching'; +import publishUserStream from '../event'; export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { // Subscribe othello stream @@ -7,4 +10,20 @@ export default function(request: websocket.request, connection: websocket.connec subscriber.on('message', (_, data) => { connection.send(data); }); + + connection.on('message', async (data) => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'ping': + if (msg.id == null) return; + const matching = await Matching.findOne({ + parent_id: user._id, + child_id: new mongo.ObjectID(msg.id) + }); + if (matching == null) return; + publishUserStream(matching.child_id, 'othello_invited', await pack(matching, matching.child_id)); + break; + } + }); } -- cgit v1.2.3-freya