sharkey/src/server/api/stream/reversi.ts

30 lines
964 B
TypeScript
Raw Normal View History

2018-03-11 18:08:26 +09:00
import * as mongo from 'mongodb';
2018-03-07 11:40:40 +09:00
import * as websocket from 'websocket';
import * as redis from 'redis';
2018-06-17 08:10:54 +09:00
import Matching, { pack } from '../../../models/reversi-matching';
2018-04-02 13:33:46 +09:00
import publishUserStream from '../../../publishers/stream';
2018-03-07 11:40:40 +09:00
export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
2018-06-17 08:10:54 +09:00
// Subscribe reversi stream
subscriber.subscribe(`misskey:reversi-stream:${user._id}`);
2018-03-07 11:40:40 +09:00
subscriber.on('message', (_, data) => {
connection.send(data);
});
2018-03-11 18:08:26 +09: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({
2018-03-29 14:48:47 +09:00
parentId: user._id,
childId: new mongo.ObjectID(msg.id)
2018-03-11 18:08:26 +09:00
});
if (matching == null) return;
2018-06-17 08:10:54 +09:00
publishUserStream(matching.childId, 'reversi_invited', await pack(matching, matching.childId));
2018-03-11 18:08:26 +09:00
break;
}
});
2018-03-07 11:40:40 +09:00
}