-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d49cbb
commit 76f8311
Showing
24 changed files
with
970 additions
and
950 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ on: | |
- main | ||
- dev | ||
- dev-be | ||
- hotfix-be-#114 | ||
jobs: | ||
build_and_deploy: | ||
runs-on: ubuntu-latest | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import * as WebSocket from 'ws'; | ||
|
||
export abstract class BaseWebSocketService { | ||
private websocket: WebSocket; | ||
private sending: boolean = false; | ||
protected readonly logger: Logger; | ||
|
||
constructor(context: string) { | ||
this.logger = new Logger(context); // 동적으로 context 설정 | ||
} | ||
|
||
protected abstract handleMessage(data: any): void; | ||
|
||
protected connectWebSocket(websocketUrl: string, reconnectInterval: number): void { | ||
this.websocket = new WebSocket(websocketUrl); | ||
|
||
this.websocket.on('open', () => { | ||
this.logger.log('WebSocket 연결이 열렸습니다.'); | ||
this.sendWebSocket(); | ||
}); | ||
|
||
this.websocket.on('message', (data) => { | ||
try { | ||
const message = JSON.parse(data.toString()); | ||
this.handleMessage(message); | ||
} catch (error) { | ||
this.logger.error('WebSocket 메시지 처리 중 오류:', error); | ||
} | ||
}); | ||
|
||
this.websocket.on('close', () => { | ||
this.logger.warn('WebSocket 연결이 닫혔습니다. 재연결 시도 중...'); | ||
setTimeout(() => this.connectWebSocket(websocketUrl, reconnectInterval), reconnectInterval); | ||
}); | ||
|
||
this.websocket.on('error', (error) => { | ||
this.logger.error('WebSocket 오류:', error); | ||
}); | ||
} | ||
|
||
protected async sendWebSocket(): Promise<void> { | ||
if (this.sending) return; | ||
this.sending = true; | ||
|
||
try { | ||
if (this.websocket.readyState !== WebSocket.OPEN) { | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
} | ||
const message = this.getSubscribeMessage(); | ||
this.websocket.send(message); | ||
} catch (error) { | ||
this.logger.error('WebSocket 메시지 전송 중 오류:', error); | ||
} finally { | ||
this.sending = false; | ||
} | ||
} | ||
|
||
protected abstract getSubscribeMessage(): string; | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/server/src/upbit/SSE/coin-ticker-websocket.service.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { SseService } from './sse.service'; | ||
import { CoinListService } from '../coin-list.service'; | ||
import { | ||
UPBIT_WEBSOCKET_URL, | ||
UPBIT_WEBSOCKET_CONNECTION_TIME, | ||
} from '@src/upbit/constants'; | ||
import { BaseWebSocketService } from './base-web-socket.service'; | ||
import { CoinDataUpdaterService } from '../coin-data-updater.service'; | ||
|
||
@Injectable() | ||
export class CoinTickerService | ||
extends BaseWebSocketService | ||
implements OnModuleInit | ||
{ | ||
constructor( | ||
private readonly coinListService: CoinListService, | ||
private readonly coinDataUpdaterService: CoinDataUpdaterService, | ||
private readonly sseService: SseService, | ||
) { | ||
super(CoinTickerService.name); | ||
} | ||
|
||
async onModuleInit() { | ||
await this.ensureCoinDataInitialized(); | ||
this.connectWebSocket(UPBIT_WEBSOCKET_URL, UPBIT_WEBSOCKET_CONNECTION_TIME); | ||
} | ||
|
||
private async ensureCoinDataInitialized(): Promise<void> { | ||
if (this.coinListService.getCoinNameList().length === 1) { | ||
await this.coinDataUpdaterService.updateCoinList(); | ||
} | ||
} | ||
|
||
protected handleMessage(data: any) { | ||
if (data.error) { | ||
console.error('CoinTicker WebSocket 오류:', data); | ||
return; | ||
} | ||
this.sseService.sendEvent('price', data); | ||
} | ||
|
||
protected getSubscribeMessage(): string { | ||
const coinList = this.coinListService.getCoinNameList(); | ||
return JSON.stringify([ | ||
{ ticket: 'test' }, | ||
{ type: 'ticker', codes: coinList }, | ||
]); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/server/src/upbit/SSE/orderbook-websocket.service.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; | ||
import { SseService } from './sse.service'; | ||
import { CoinListService } from '../coin-list.service'; | ||
import { | ||
UPBIT_WEBSOCKET_URL, | ||
UPBIT_WEBSOCKET_CONNECTION_TIME, | ||
} from '@src/upbit/constants'; | ||
import { BaseWebSocketService } from './base-web-socket.service'; | ||
import { CoinDataUpdaterService } from '../coin-data-updater.service'; | ||
|
||
@Injectable() | ||
export class OrderbookService | ||
extends BaseWebSocketService | ||
implements OnModuleInit | ||
{ | ||
constructor( | ||
private readonly coinListService: CoinListService, | ||
private readonly coinDataUpdaterService: CoinDataUpdaterService, | ||
private readonly sseService: SseService, | ||
) { | ||
super(OrderbookService.name); | ||
} | ||
|
||
async onModuleInit() { | ||
await this.ensureCoinDataInitialized(); | ||
this.connectWebSocket(UPBIT_WEBSOCKET_URL, UPBIT_WEBSOCKET_CONNECTION_TIME); | ||
} | ||
|
||
private async ensureCoinDataInitialized(): Promise<void> { | ||
if (this.coinListService.getCoinNameList().length === 1) { | ||
await this.coinDataUpdaterService.updateCoinList(); | ||
} | ||
} | ||
|
||
protected handleMessage(data: any) { | ||
if (data.error) { | ||
this.logger.error('Orderbook WebSocket 오류:', data); | ||
return; | ||
} | ||
this.sseService.sendEvent('orderbook', data); | ||
} | ||
|
||
protected getSubscribeMessage(): string { | ||
const coinList = this.coinListService.getCoinNameList(); | ||
return JSON.stringify([ | ||
{ ticket: 'test' }, | ||
{ type: 'orderbook', codes: coinList }, | ||
]); | ||
} | ||
} |
Oops, something went wrong.