Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ping pong #17

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/realms/rest/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const emitter = new EventEmitter() as Emitter<EmitterMessage>
emitter.setMaxListeners(0)

export enum EmitterMessageTypes {
ping = "ping",
pong = "pong",

setSubscriptions = "setSubscriptions",
hello = "hello",

Expand All @@ -18,6 +21,14 @@ export const emitterMessageTypes = Type.Enum(EmitterMessageTypes)

export const emitterMessage = Type.Union(
[
Type.Object({
type: Type.Literal("ping"),
payload: Type.Null()
}),
Type.Object({
type: Type.Literal("pong"),
payload: Type.Null()
}),
Type.Object({
type: Type.Literal("setSubscriptions"),
payload: Type.Object({
Expand All @@ -32,7 +43,7 @@ export const emitterMessage = Type.Union(
}),
Type.Object({
type: Type.Literal("serverPlayersChanged"),
payload: Type.Object({})
payload: Type.Null()
})
],
{
Expand Down
4 changes: 2 additions & 2 deletions src/realms/rest/endpoints/controllers/Player.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default class PlayerController {
const fetchedPlayer = await this.playerService.disconnectPlayer(
req.params.uuid
)
emitter.emit("serverPlayersChanged", {})
emitter.emit("serverPlayersChanged", null)
return reply.code(200).send(fetchedPlayer)
}

Expand Down Expand Up @@ -192,7 +192,7 @@ export default class PlayerController {
} catch (_) {
/* empty */
}
emitter.emit("serverPlayersChanged", {})
emitter.emit("serverPlayersChanged", null)
return reply.code(200).send(serverPlayer)
}

Expand Down
11 changes: 11 additions & 0 deletions src/realms/rest/endpoints/services/Misc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
emitter
} from "../../emitter"
import { AnimusRestServer } from "../.."
import { WebSocket } from "ws"

@Service()
export default class MiscService {
Expand Down Expand Up @@ -41,6 +42,8 @@ export default class MiscService {
this.websockets.set(connection, subscriptions)
} else if (body.type === "hello") {
emitter.emit("hello", { ok: true })
} else if (body.type === "ping") {
connection.socket.send(JSON.stringify({ type: "pong" }))
}
} catch (e) {
connection.socket.off("message", handleMessage)
Expand All @@ -56,5 +59,13 @@ export default class MiscService {
.getLogger()
.debug("Websocket connection closed")
})

const pingInterval = setInterval(() => {
if (connection.socket.readyState !== WebSocket.OPEN) {
clearInterval(pingInterval)
return
}
connection.socket.send(JSON.stringify({ type: "ping" }))
}, 10000)
}
}
Loading