-
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.
Merge pull request #56 from boostcampwm-2024/feature-be-#52
- Loading branch information
Showing
12 changed files
with
312 additions
and
106 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,6 +6,8 @@ on: | |
- main | ||
- dev | ||
- dev-be | ||
- feature-be-#52 | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export const UPBIT_WEBSOCKET_URL = 'wss://api.upbit.com/websocket/v1'; | ||
export const UPBIT_RESTAPI_URL = 'https://api.upbit.com/v1/market/all?is_details=true' | ||
export const UPBIT_IMAGE_URL = "https://static.upbit.com/logos/" | ||
|
||
export const UPBIT_WEBSOCKET_CONNECTION_TIME = 3000 | ||
export const UPBIT_IMAGE_URL = "https://static.upbit.com/logos/" | ||
export const UPBIT_UPDATED_COIN_INFO_TIME = 5000 | ||
export const UPBIT_UPDATED_ORDER_INFO_TIME = 1000 |
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
18 changes: 18 additions & 0 deletions
18
packages/server/src/upbit/coin-ticker-websocket.service.spec.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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CoinTickerService } from './coin-ticker-websocket.service'; | ||
|
||
describe('CoinTickerService', () => { | ||
let service: CoinTickerService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CoinTickerService], | ||
}).compile(); | ||
|
||
service = module.get<CoinTickerService>(CoinTickerService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
packages/server/src/upbit/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,80 @@ | ||
import { Injectable, OnModuleInit } from '@nestjs/common'; | ||
import * as WebSocket from 'ws'; | ||
import { SseService } from './sse.service'; | ||
import { CoinListService } from './coin-list.service'; | ||
import { | ||
UPBIT_UPDATED_COIN_INFO_TIME, | ||
UPBIT_WEBSOCKET_CONNECTION_TIME, | ||
UPBIT_WEBSOCKET_URL, | ||
} from 'common/upbit'; | ||
import { CoinTickerDto } from './dtos/coin-ticker.dto'; | ||
|
||
@Injectable() | ||
export class CoinTickerService implements OnModuleInit { | ||
private websocket: WebSocket; | ||
private sending: Boolean = false; | ||
private timeoutId: NodeJS.Timeout | null = null; | ||
private coinLatestInfo = new Map(); | ||
constructor( | ||
private readonly coinListService: CoinListService, | ||
private readonly sseService: SseService, | ||
) {} | ||
|
||
onModuleInit() { | ||
this.websocket = new WebSocket(UPBIT_WEBSOCKET_URL); | ||
this.connectWebSocket(); | ||
} | ||
|
||
connectWebSocket() { | ||
this.websocket.on('open', () => { | ||
try { | ||
console.log('CoinTickerWebSocket 연결이 열렸습니다.'); | ||
this.sendWebSocket(); | ||
} catch (error) { | ||
console.error('sendWebSocket 실행 중 오류 발생:', error); | ||
} | ||
}); | ||
this.websocket.on('message', (data) => { | ||
try{ | ||
const message = JSON.parse(data.toString()); | ||
this.sseService.coinTickerData(message); | ||
this.sseService.setCoinLastestInfo(message); | ||
}catch(error){ | ||
console.error('CoinTickerWebSocket 오류:', error); | ||
} | ||
}); | ||
this.websocket.on('close', () => { | ||
try { | ||
console.log('CoinTickerWebSocket 연결이 닫혔습니다. 재연결 시도 중...'); | ||
setTimeout( | ||
() => this.connectWebSocket(), | ||
UPBIT_WEBSOCKET_CONNECTION_TIME | ||
); | ||
} catch (error) { | ||
console.error('WebSocket 재연결 설정 중 오류 발생:', error); | ||
} | ||
}); | ||
|
||
this.websocket.on('error', (error) => { | ||
console.error('CoinTickerWebSocket 오류:', error); | ||
}); | ||
} | ||
async sendWebSocket() { | ||
if (this.sending) return; | ||
this.sending = true; | ||
try{ | ||
const coin_list = this.coinListService.getCoinNameList(); | ||
const subscribeMessage = JSON.stringify([ | ||
{ ticket: 'test' }, | ||
{ type: 'ticker', codes: coin_list }, | ||
]); | ||
this.websocket.send(subscribeMessage); | ||
}catch(error){ | ||
console.error('CoinTickerWebSocket 오류:', error); | ||
}finally{ | ||
this.sending = false; | ||
if (this.timeoutId) clearTimeout(this.timeoutId); | ||
this.timeoutId = setTimeout(() => this.sendWebSocket(), UPBIT_UPDATED_COIN_INFO_TIME); | ||
} | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
...es/server/src/upbit/upbit.service.spec.ts → ...upbit/orderbook-websocket.service.spec.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
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,72 @@ | ||
import { Injectable, OnModuleInit } from '@nestjs/common'; | ||
import * as WebSocket from 'ws'; | ||
import { SseService } from './sse.service'; | ||
import { CoinListService } from './coin-list.service'; | ||
import { UPBIT_WEBSOCKET_CONNECTION_TIME, UPBIT_WEBSOCKET_URL, UPBIT_UPDATED_COIN_INFO_TIME, UPBIT_UPDATED_ORDER_INFO_TIME } from 'common/upbit'; | ||
import { CoinTickerDto } from './dtos/coin-ticker.dto'; | ||
|
||
@Injectable() | ||
export class OrderbookService implements OnModuleInit{ | ||
private websocket: WebSocket; | ||
private sending: Boolean = false; | ||
private timeoutId: NodeJS.Timeout | null = null; | ||
|
||
constructor( | ||
private readonly coinListService: CoinListService, | ||
private readonly sseService: SseService | ||
) {}; | ||
|
||
onModuleInit() { | ||
this.websocket = new WebSocket(UPBIT_WEBSOCKET_URL); | ||
this.connectWebSocket() | ||
} | ||
|
||
connectWebSocket() { | ||
this.websocket.on('open', () => { | ||
try { | ||
console.log('OrderbookWebSocket 연결이 열렸습니다.'); | ||
this.sendWebSocket(); | ||
} catch (error) { | ||
console.error('sendWebSocket 실행 중 오류 발생:', error); | ||
} | ||
}); | ||
this.websocket.on('message', (data) => { | ||
try { | ||
const message = JSON.parse(data.toString()); | ||
this.sseService.orderbookData(message); | ||
} catch (error) { | ||
console.error('OrderbookWebSocket 메시지 처리 중 오류 발생:', error); | ||
} | ||
}); | ||
this.websocket.on('close', () => { | ||
try { | ||
console.log('OrderbookWebSocket 연결이 닫혔습니다. 재연결 시도 중...'); | ||
setTimeout(() => this.connectWebSocket(), UPBIT_WEBSOCKET_CONNECTION_TIME); | ||
} catch (error) { | ||
console.error('OrderbookWebSocket 재연결 설정 중 오류 발생:', error); | ||
} | ||
}); | ||
|
||
this.websocket.on('error', (error) => { | ||
console.error('OrderbookWebSocket 오류:', error); | ||
}); | ||
} | ||
async sendWebSocket() { | ||
if (this.sending) return; | ||
this.sending = true; | ||
try{ | ||
const coin_list = this.coinListService.getCoinNameList(); | ||
const subscribeMessage = JSON.stringify([ | ||
{ ticket: 'test' }, | ||
{ type: 'ticker', codes: coin_list }, | ||
]); | ||
this.websocket.send(subscribeMessage); | ||
}catch(error){ | ||
console.error('OrderbookWebSocket 오류:', error); | ||
}finally{ | ||
this.sending = false; | ||
if (this.timeoutId) clearTimeout(this.timeoutId); | ||
this.timeoutId = setTimeout(() => this.sendWebSocket(), UPBIT_UPDATED_ORDER_INFO_TIME); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.