Skip to content

Commit

Permalink
feat: add socket request feature
Browse files Browse the repository at this point in the history
  • Loading branch information
seo-rii committed Dec 28, 2021
1 parent 72a1c45 commit 9da8dfe
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/judge.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {sendMessage} from "./socket";
import type {Problem} from "./types/problem";
import {WebSocketResponseType} from "./types/response";


const waitList = [] as Problem[]

function judgeFinishHandler() {
const problem = waitList.shift()
if (problem) {
sendMessage(true, 'JUDGE_FINISH', {
sendMessage(WebSocketResponseType.JUDGE_FINISH, {
uid: problem.uid,
score: 100,
reason: 'AC'
Expand Down
45 changes: 39 additions & 6 deletions src/socket.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import {WebSocket} from "ws";
import expressWs from "express-ws";
import {WebSocketResponse, WebSocketResponseType} from "./types/response";
import {WebSocketRequest, WebSocketRequestType} from "./types/request";
import {getJudgeInfo} from "./judge";

let wsObject = null as WebSocket | null

export function sendMessage(success: boolean, type: WebSocketResponseType, data: any) {
export function sendMessage(type: WebSocketResponseType, data?: any) {
if (wsObject) {
wsObject.send(JSON.stringify({
success,
success: true,
type,
data
}));
} as WebSocketResponse));
}
}

export function sendError(reason: string) {
if (wsObject) {
wsObject.send(JSON.stringify({
success: false,
error: reason
} as WebSocketResponse));
}
}

Expand All @@ -22,9 +33,31 @@ export function init(app: expressWs.Application) {
}
wsObject = ws
ws.on('message', function (msg) {
ws.on('disconnect', function () {
wsObject = null
});
try {
const message = JSON.parse(msg.toString()) as WebSocketRequest
if (!message.type) {
sendError('Invalid message')
return
}
switch (message.type) {
case WebSocketRequestType.JUDGE_INFO:
sendMessage(WebSocketResponseType.JUDGE_INFO, {
version: "1.0.0"
})
break
case WebSocketRequestType.JUDGE_STATUS:
sendMessage(WebSocketResponseType.JUDGE_INFO, getJudgeInfo())
break
default:
sendError('Invalid message')
break
}
} catch (e) {
sendError('JSON Parse Error')
}
});
ws.on('disconnect', function () {
wsObject = null
});
});
}
10 changes: 9 additions & 1 deletion src/types/request.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export type WebSocketRequestType = 'JUDGE_INFO' | 'JUDGE_STATUS'
export const enum WebSocketRequestType {
JUDGE_INFO = 'JUDGE_INFO',
JUDGE_STATUS = 'JUDGE_STATUS',
}

export interface WebSocketRequest {
type: WebSocketRequestType
data?: any
}
10 changes: 8 additions & 2 deletions src/types/response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export type WebSocketResponseType = 'JUDGE_FINISH'
export const enum WebSocketResponseType {
JUDGE_FINISH = 'JUDGE_FINISH',
JUDGE_PROGRESS = 'JUDGE_PROGRESS',
JUDGE_INFO = 'JUDGE_INFO',
JUDGE_STATUS = 'JUDGE_STATUS',
JUDGE_ERROR = 'JUDGE_ERROR',
}

export interface WebSocketResponse {
success: boolean
type: WebSocketResponseType
data: any
data?: any
error?: string
}

0 comments on commit 9da8dfe

Please sign in to comment.