perf(backend): use websockets/ws instead of theturtle32/WebSocket-Node (#10884)

* perf(backend): use websockets/ws instead of theturtle32/WebSocket-Node

Resolve #10883

* refactor

* Update StreamingApiServerService.ts

* Update StreamingApiServerService.ts

* ✌️

* Update StreamingApiServerService.ts

* fix main stream init

* fix timing 2

* setIntervalの重複を避ける(気休め)

* add comment

* ✌️

---------

Co-authored-by: tamaina <tamaina@hotmail.co.jp>
This commit is contained in:
syuilo
2023-05-29 13:32:19 +09:00
committed by GitHub
parent b35b9bc27f
commit f930eaee02
6 changed files with 107 additions and 108 deletions

View File

@@ -1,3 +1,4 @@
import * as WebSocket from 'ws';
import type { User } from '@/models/entities/User.js';
import type { AccessToken } from '@/models/entities/AccessToken.js';
import type { Packed } from '@/misc/json-schema.js';
@@ -7,7 +8,6 @@ import { bindThis } from '@/decorators.js';
import { CacheService } from '@/core/CacheService.js';
import { UserProfile } from '@/models/index.js';
import type { ChannelsService } from './ChannelsService.js';
import type * as websocket from 'websocket';
import type { EventEmitter } from 'events';
import type Channel from './channel.js';
import type { StreamEventEmitter, StreamMessages } from './types.js';
@@ -18,7 +18,7 @@ import type { StreamEventEmitter, StreamMessages } from './types.js';
export default class Connection {
public user?: User;
public token?: AccessToken;
private wsConnection: websocket.connection;
private wsConnection: WebSocket.WebSocket;
public subscriber: StreamEventEmitter;
private channels: Channel[] = [];
private subscribingNotes: any = {};
@@ -37,11 +37,9 @@ export default class Connection {
private notificationService: NotificationService,
private cacheService: CacheService,
subscriber: EventEmitter,
user: User | null | undefined,
token: AccessToken | null | undefined,
) {
this.subscriber = subscriber;
if (user) this.user = user;
if (token) this.token = token;
}
@@ -70,12 +68,16 @@ export default class Connection {
if (this.user != null) {
await this.fetch();
this.fetchIntervalId = setInterval(this.fetch, 1000 * 10);
if (!this.fetchIntervalId) {
this.fetchIntervalId = setInterval(this.fetch, 1000 * 10);
}
}
}
@bindThis
public async init2(wsConnection: websocket.connection) {
public async listen(subscriber: EventEmitter, wsConnection: WebSocket.WebSocket) {
this.subscriber = subscriber;
this.wsConnection = wsConnection;
this.wsConnection.on('message', this.onWsConnectionMessage);
@@ -88,14 +90,11 @@ export default class Connection {
* クライアントからメッセージ受信時
*/
@bindThis
private async onWsConnectionMessage(data: websocket.Message) {
if (data.type !== 'utf8') return;
if (data.utf8Data == null) return;
private async onWsConnectionMessage(data: WebSocket.RawData) {
let obj: Record<string, any>;
try {
obj = JSON.parse(data.utf8Data);
obj = JSON.parse(data.toString());
} catch (e) {
return;
}