Skip to content

Commit

Permalink
Merge pull request #85 from boostcampwm-2024/fix/socket
Browse files Browse the repository at this point in the history
[BE] 소켓 분리 작업 추가
  • Loading branch information
sieunie authored Nov 12, 2024
2 parents d32a04c + 4c1250a commit eca8560
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
5 changes: 3 additions & 2 deletions BE/src/websocket/base-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SocketTokenService } from './socket-token.service';
@Injectable()
export abstract class BaseSocketService implements OnModuleInit {
protected socket: WebSocket;
protected abstract tradeHandler: Record<string, (data: string) => void>;
protected socketConnectionKey: string;

constructor(
Expand All @@ -30,11 +29,13 @@ export abstract class BaseSocketService implements OnModuleInit {
: JSON.stringify(event.data);
if (data.length < 2) return;

(this.tradeHandler[data[1]] as (data) => void)(data[3]);
const dataList = data[3].split('^');
this.handleSocketData(dataList);
};
}

protected abstract handleSocketOpen(): void;
protected abstract handleSocketData(dataList): void;

protected registerCode(trId: string, trKey: string) {
this.socket.send(
Expand Down
17 changes: 6 additions & 11 deletions BE/src/websocket/stock-index-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { BaseSocketService } from './base-socket.service';

@Injectable()
export class StockIndexSocketService extends BaseSocketService {
protected tradeHandler = {
H0UPCNT0: this.handleStockIndexValue.bind(this),
};

private STOCK_CODE = {
'0001': 'KOSPI',
'1001': 'KOSDAQ',
Expand All @@ -22,15 +18,14 @@ export class StockIndexSocketService extends BaseSocketService {
this.registerCode('H0UPCNT0', '3003'); // KSQ150
}

private handleStockIndexValue(responseData: string) {
const responseList = responseData.split('^');
protected handleSocketData(dataList: string[]) {
this.socketGateway.sendStockIndexValueToClient(
this.STOCK_CODE[responseList[0]],
this.STOCK_CODE[dataList[0]],
new StockIndexValueElementDto(
responseList[2],
responseList[4],
responseList[9],
responseList[3],
dataList[2],
dataList[4],
dataList[9],
dataList[3],
),
);
}
Expand Down

0 comments on commit eca8560

Please sign in to comment.