import { ProtocolRequest, ProtocolResponse, ProtocolEncoder, PacketBody, ProtocolDecoder, ProtocolMessage, ProtocolStream, PacketBodyValue, BodyStringDivider } from '@ucap-webmessenger/protocol'; import { EventType } from '../types/event.type'; import { Info } from '../models/info'; export interface InfoRequest extends ProtocolRequest { // 대화방SEQ(s) roomSeq: string; // 기준이벤트SEQ(n) baseSeq: number; // 요청이벤트개수(n) requestCount: number; } export interface InfoData extends ProtocolStream { // 대화방SEQ(s) roomSeq: string; infoList: Info[]; } export interface InfoResponse extends ProtocolResponse { // 대화방SEQ(s) roomSeq: string; // 기준이벤트SEQ(n) baseSeq: number; // 유효한파일기준이벤트SEQ(n) validFileBaseSeq: number; // 이벤트정보 개수(n) count: number; } export const encodeInfo: ProtocolEncoder = (req: InfoRequest) => { const bodyList: PacketBody[] = []; bodyList.push( { type: PacketBodyValue.String, value: req.roomSeq }, { type: PacketBodyValue.Integer, value: req.baseSeq }, { type: PacketBodyValue.Integer, value: req.requestCount } ); return bodyList; }; export const decodeInfoData: ProtocolDecoder = ( message: ProtocolMessage ) => { const infoList: Info[] = []; for (const body of message.bodyList) { const info = body.split(BodyStringDivider); if (info.length > 5) { infoList.push({ seq: info[0], type: info[1] as EventType, senderSeq: Number(info[2]), sendDate: new Date(info[3]), sentMessage: info[4], receiverCount: Number(info[5]) }); } } return { roomSeq: message.bodyList[0], infoList } as InfoData; }; export const decodeInfo: ProtocolDecoder = ( message: ProtocolMessage ) => { return { roomSeq: message.bodyList[0], baseSeq: message.bodyList[1], validFileBaseSeq: message.bodyList[2], count: message.bodyList[3] } as InfoResponse; };