Skip to content

Commit

Permalink
Streaming接続イベントのログ Resolve #1664
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Sep 28, 2020
1 parent b272075 commit ab287e0
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/server/api/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { ParsedUrlQuery } from 'querystring';
import authenticate from './authenticate';
import { EventEmitter } from 'events';
import config from '../../config';
import rndstr from 'rndstr';
import Logger from '../../services/logger';

export const streamLogger = new Logger('stream', 'cyan');

module.exports = (server: http.Server) => {
// Init websocket server
Expand All @@ -21,6 +25,11 @@ module.exports = (server: http.Server) => {

const connection = request.accept();

const connHash = rndstr(8);
const connPeer = `${connection?.remoteAddress} (xff=${request?.httpRequest?.headers['x-forwarded-for']})`;
const connUser = user ? `${user._id} (${user.username})` : 'anonymous';
streamLogger.info(`connect ${connHash} (${connPeer} ${connUser})`);

let ev: EventEmitter;

if (config.redis) {
Expand Down Expand Up @@ -53,34 +62,15 @@ module.exports = (server: http.Server) => {

const main = new MainStreamConnection(connection, ev, user, app);

// 後方互換性のため
//#region 後方互換性のため
if (request.resourceURL.pathname !== '/streaming') {
main.sendMessageToWsOverride = (type: string, payload: any) => {
if (type == 'channel') {
type = payload.type;
payload = payload.body;
}
if (type.startsWith('api:')) {
type = type.replace('api:', 'api-res:');
}
connection.send(JSON.stringify({
type: type,
body: payload
}));
};

main.connectChannel(Math.random().toString().substr(2, 8), null,
request.resourceURL.pathname === '/' ? 'homeTimeline' :
request.resourceURL.pathname === '/local-timeline' ? 'localTimeline' :
request.resourceURL.pathname === '/hybrid-timeline' ? 'hybridTimeline' :
request.resourceURL.pathname === '/global-timeline' ? 'globalTimeline' : null);

if (request.resourceURL.pathname === '/') {
main.connectChannel(Math.random().toString().substr(2, 8), null, 'main');
}
streamLogger.warn(`404 ${request.resourceURL.pathname} ${connHash} (${connPeer} ${connUser})`);
connection.close(404);
}
//#endregion 後方互換性のため

connection.once('close', () => {
streamLogger.info(`close ${connHash} (${connPeer} ${connUser})`);
ev.removeAllListeners();
main.dispose();
});
Expand All @@ -90,5 +80,5 @@ module.exports = (server: http.Server) => {
connection.send('pong');
}
});
});
}); // ws on request
};

0 comments on commit ab287e0

Please sign in to comment.