diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-10-11 18:09:41 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-10-11 18:09:41 +0900 |
| commit | 05f9ad11bb51ca932cba7716302924dfaa7aafdd (patch) | |
| tree | 1a0f914a589cf01d3f7b26919269725222ffaded /src/server/api | |
| parent | 10.7.2 (diff) | |
| download | sharkey-05f9ad11bb51ca932cba7716302924dfaa7aafdd.tar.gz sharkey-05f9ad11bb51ca932cba7716302924dfaa7aafdd.tar.bz2 sharkey-05f9ad11bb51ca932cba7716302924dfaa7aafdd.zip | |
Redisがインストールされているときはイベントの共有にRedisのpub/subを使うように
Diffstat (limited to 'src/server/api')
| -rw-r--r-- | src/server/api/stream/index.ts | 6 | ||||
| -rw-r--r-- | src/server/api/streaming.ts | 32 |
2 files changed, 33 insertions, 5 deletions
diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts index 838d847004..afedd4362c 100644 --- a/src/server/api/stream/index.ts +++ b/src/server/api/stream/index.ts @@ -1,6 +1,5 @@ import autobind from 'autobind-decorator'; import * as websocket from 'websocket'; -import Xev from 'xev'; import * as debug from 'debug'; import User, { IUser } from '../../../models/user'; @@ -11,6 +10,7 @@ import readNote from '../../../services/note/read'; import Channel from './channel'; import channels from './channels'; +import { EventEmitter } from 'events'; const log = debug('misskey'); @@ -21,14 +21,14 @@ export default class Connection { public user?: IUser; public app: IApp; private wsConnection: websocket.connection; - public subscriber: Xev; + public subscriber: EventEmitter; private channels: Channel[] = []; private subscribingNotes: any = {}; public sendMessageToWsOverride: any = null; // 後方互換性のため constructor( wsConnection: websocket.connection, - subscriber: Xev, + subscriber: EventEmitter, user: IUser, app: IApp ) { diff --git a/src/server/api/streaming.ts b/src/server/api/streaming.ts index a0a219a317..249157e222 100644 --- a/src/server/api/streaming.ts +++ b/src/server/api/streaming.ts @@ -1,11 +1,14 @@ import * as http from 'http'; import * as websocket from 'websocket'; +import * as redis from 'redis'; import Xev from 'xev'; import MainStreamConnection from './stream'; import { ParsedUrlQuery } from 'querystring'; import authenticate from './authenticate'; import channels from './stream/channels'; +import { EventEmitter } from 'events'; +import config from '../../config'; module.exports = (server: http.Server) => { // Init websocket server @@ -16,11 +19,36 @@ module.exports = (server: http.Server) => { ws.on('request', async (request) => { const connection = request.accept(); - const ev = new Xev(); - const q = request.resourceURL.query as ParsedUrlQuery; const [user, app] = await authenticate(q.i as string); + let ev: EventEmitter; + + if (config.redis) { + // Connect to Redis + const subscriber = redis.createClient( + config.redis.port, config.redis.host); + + subscriber.subscribe('misskey'); + + ev = new EventEmitter(); + + subscriber.on('message', async (_, data) => { + const obj = JSON.parse(data); + + console.log(obj); + + ev.emit(obj.channel, obj.message); + }); + + connection.once('close', () => { + subscriber.unsubscribe(); + subscriber.quit(); + }); + } else { + ev = new Xev(); + } + const main = new MainStreamConnection(connection, ev, user, app); // 後方互換性のため |