summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/stream/index.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-05-29 13:32:19 +0900
committerGitHub <noreply@github.com>2023-05-29 13:32:19 +0900
commitf930eaee020520c1f9496803dcf445b2f2955526 (patch)
tree72c14dffbcb1c8cf7ed52a835c7e3e3fd03e4f68 /packages/backend/src/server/api/stream/index.ts
parentUpdate QueueProcessorService.ts (diff)
downloadsharkey-f930eaee020520c1f9496803dcf445b2f2955526.tar.gz
sharkey-f930eaee020520c1f9496803dcf445b2f2955526.tar.bz2
sharkey-f930eaee020520c1f9496803dcf445b2f2955526.zip
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 * :v: * Update StreamingApiServerService.ts * fix main stream init * fix timing 2 * setIntervalの重複を避ける(気休め) * add comment * :v: --------- Co-authored-by: tamaina <tamaina@hotmail.co.jp>
Diffstat (limited to 'packages/backend/src/server/api/stream/index.ts')
-rw-r--r--packages/backend/src/server/api/stream/index.ts21
1 files changed, 10 insertions, 11 deletions
diff --git a/packages/backend/src/server/api/stream/index.ts b/packages/backend/src/server/api/stream/index.ts
index fee56e3668..8b1c2c09c9 100644
--- a/packages/backend/src/server/api/stream/index.ts
+++ b/packages/backend/src/server/api/stream/index.ts
@@ -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;
}