-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* 🐛 fix: join column에 user_id 이름 추가 * 🐛 fix: websocketclient에러 수정 * 🐛 fix: join column 롤백 * ♻️ refactor: 알림 수 200개 제한 해제 * 💄 style: 테스트 코드 삭제 * 📝 docs: request volum + e * 📝 docs: apiresponse status 201 정상화
- Loading branch information
1 parent
830dcd1
commit 2a6d07a
Showing
5 changed files
with
29 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 17 additions & 17 deletions
34
packages/backend/src/scraper/openapi/websocket/websocketClient.websocket.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,68 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Logger } from 'winston'; | ||
import { RawData, WebSocket } from 'ws'; | ||
|
||
@Injectable() | ||
export class WebsocketClient { | ||
static url = process.env.WS_URL ?? 'ws://ops.koreainvestment.com:21000'; | ||
private client: WebSocket; | ||
//현재 factory 패턴을 이용해 할당하면 socket이 열리기 전에 message가 가는 문제가 있음. | ||
// 소켓이 할당되기 전에(client에 소켓이 없을 때) message를 보내려 시도함. | ||
private messageQueue: string[] = []; | ||
|
||
constructor(@Inject('winston') private readonly logger: Logger) { | ||
this.client = new WebSocket(WebsocketClient.url); | ||
this.initOpen(() => this.flushQueue()); | ||
this.initError((error) => this.logger.error('WebSocket error', error)); | ||
} | ||
|
||
static websocketFactory(logger: Logger) { | ||
const websocket = new WebsocketClient(logger); | ||
return websocket; | ||
return new WebsocketClient(logger); | ||
} | ||
|
||
subscribe(message: string) { | ||
this.sendMessage(message); | ||
} | ||
|
||
unsubscribe(message: string) { | ||
this.sendMessage(message); | ||
} | ||
|
||
private initOpen(fn: () => void) { | ||
this.client.on('open', fn); | ||
} | ||
|
||
private initMessage(fn: (data: RawData) => void) { | ||
this.client.on('message', fn); | ||
} | ||
|
||
private initDisconnect(initCloseCallback: () => void) { | ||
this.client.on('close', initCloseCallback); | ||
} | ||
|
||
private initError(initErrorCallback: (error: unknown) => void) { | ||
this.client.on('error', initErrorCallback); | ||
} | ||
|
||
connectFacade( | ||
initOpenCallback: (fn: (message: string) => void) => () => void, | ||
initMessageCallback: (client: WebSocket) => (data: RawData) => void, | ||
initCloseCallback: () => void, | ||
initErrorCallback: (error: unknown) => void, | ||
) { | ||
this.initOpen(initOpenCallback(this.sendMessage)); | ||
this.initOpen(initOpenCallback(this.sendMessage.bind(this))); | ||
this.initMessage(initMessageCallback(this.client)); | ||
this.initDisconnect(initCloseCallback); | ||
this.initError(initErrorCallback); | ||
} | ||
|
||
private sendMessage(message: string) { | ||
if (!this.client || !this.client.readyState) { | ||
this.logger.warn('WebSocket is not open. Message not sent. '); | ||
return; | ||
} | ||
if (this.client.readyState === WebSocket.OPEN) { | ||
this.client.send(message); | ||
this.logger.info(`Sent message: ${message}`); | ||
} else { | ||
this.logger.warn('WebSocket is not open. Message not sent. '); | ||
this.logger.warn('WebSocket not open. Queueing message.'); | ||
this.messageQueue.push(message); // 큐에 메시지를 추가 | ||
} | ||
} | ||
|
||
private flushQueue() { | ||
while (this.messageQueue.length > 0) { | ||
const message = this.messageQueue.shift(); | ||
if (message) { | ||
this.sendMessage(message); | ||
} | ||
} | ||
} | ||
} |