Sharkey/src/server/api/stream/othello.ts

30 lines
944 B
TypeScript
Raw Normal View History

2018-03-11 11:08:26 +02:00
import * as mongo from 'mongodb';
2018-03-07 04:40:40 +02:00
import * as websocket from 'websocket';
import * as redis from 'redis';
2018-03-11 11:08:26 +02:00
import Matching, { pack } from '../models/othello-matching';
import publishUserStream from '../event';
2018-03-07 04:40:40 +02:00
export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
2018-03-07 10:48:32 +02:00
// Subscribe othello stream
subscriber.subscribe(`misskey:othello-stream:${user._id}`);
2018-03-07 04:40:40 +02:00
subscriber.on('message', (_, data) => {
connection.send(data);
});
2018-03-11 11:08:26 +02:00
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;
}
});
2018-03-07 04:40:40 +02:00
}