yumechi-no-kuni/src/server/api/stream/messaging.ts

26 lines
746 B
TypeScript
Raw Normal View History

2016-12-28 16:49:51 -06:00
import * as websocket from 'websocket';
2018-07-29 17:20:27 -05:00
import Xev from 'xev';
2017-06-12 12:12:30 -05:00
import read from '../common/read-messaging-message';
2018-03-29 06:32:18 -05:00
import { ParsedUrlQuery } from 'querystring';
2016-12-28 16:49:51 -06:00
2018-07-29 17:20:27 -05:00
export default function(request: websocket.request, connection: websocket.connection, subscriber: Xev, user: any): void {
2018-03-29 06:32:18 -05:00
const q = request.resourceURL.query as ParsedUrlQuery;
const otherparty = q.otherparty as string;
2016-12-28 16:49:51 -06:00
// Subscribe messaging stream
2018-07-29 17:20:27 -05:00
subscriber.on(`messaging-stream:${user._id}-${otherparty}`, data => {
connection.send(JSON.stringify(data));
2016-12-28 16:49:51 -06:00
});
connection.on('message', async (data) => {
const msg = JSON.parse(data.utf8Data);
switch (msg.type) {
case 'read':
2017-06-12 12:12:30 -05:00
if (!msg.id) return;
read(user._id, otherparty, msg.id);
2016-12-28 16:49:51 -06:00
break;
}
});
}