Skip to content

Commit

Permalink
chore: ws server rework (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 authored Dec 2, 2024
1 parent 44c00a5 commit 97b51a9
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 88 deletions.
58 changes: 31 additions & 27 deletions apps/website/server/api/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { WebSocketEvents } from '@chat-game/types'
import type { Room } from '~~/types/room'
import { createId } from '@paralleldrive/cuid2'
import { Room } from '../utils/room'
import { AddonRoom } from '../core/rooms/addon'

const logger = useLogger('ws')
export const activeRooms: Room[] = []
Expand All @@ -10,7 +11,9 @@ export function sendMessage(message: WebSocketEvents, token: string): void {

for (const room of rooms) {
const preparedMessage = JSON.stringify({ id: createId(), ...message })
room.server.send(preparedMessage)
if (room.server.peer?.id) {
room.server.peer.publish(room.id, preparedMessage)
}
}
}

Expand All @@ -37,52 +40,53 @@ export default defineWebSocketHandler({
const token = parsed.data.token
// Create if not exist
if (!activeRooms.find((room) => room.token === token)) {
activeRooms.push(new Room({ id, token, type: 'ADDON' }))
activeRooms.push(new AddonRoom({ id, token }))
}

const activeRoom = activeRooms.find((room) => room.token === token) as Room
const activeRoom = activeRooms.find((room) => room.token === token) as AddonRoom

peer.subscribe(activeRoom.id)
logger.log(`peer subscribed to room id ${activeRoom.id}`, peer.id)
logger.log(`Peer ${peer.id} subscribed to AddonRoom ${activeRoom.id}`)
}

if (client === 'GAME') {
const token = parsed.data.token
// Create if not exist
if (!activeRooms.find((room) => room.token === token)) {
activeRooms.push(new Room({ id, token, type: 'GAME' }))
}
// if (client === 'GAME') {
// const token = parsed.data.token
// // Create if not exist
// if (!activeRooms.find((room) => room.token === token)) {
// activeRooms.push(new Room({ id, token, type: 'GAME' }))
// }

const activeRoom = activeRooms.find((room) => room.token === token) as Room
if (!activeRoom.peers.includes(peer.id)) {
activeRoom.peers.push(peer.id)
}
// const activeRoom = activeRooms.find((room) => room.token === token) as Room
// if (!activeRoom.peers.includes(peer.id)) {
// activeRoom.peers.push(peer.id)
// }

peer.subscribe(activeRoom.id)
logger.log(`peer subscribed to room id ${activeRoom.id}`, peer.id)
}
// peer.subscribe(activeRoom.id)
// logger.log(`peer subscribed to room id ${activeRoom.id}`, peer.id)
// }

if (client === 'SERVER') {
const activeRoom = activeRooms.find((room) => room.id === id)
if (!activeRoom) {
return
}

activeRoom.server.peer = peer
peer.subscribe(activeRoom.id)
logger.log(`server subscribed to room id ${activeRoom.id}`, peer.id)
logger.log(`Server subscribed to Room ${activeRoom.id}`, peer.id)
}
}
if (parsed.type === 'TEST') {
peer.publish(parsed.data.id, JSON.stringify({ id: createId(), type: 'TEST' }))
}
if (parsed.type === 'NEW_TREE') {
const room = activeRooms.find((room) => room.peers.find((id) => id === peer.id))
if (!room) {
return
}
peer.publish(room.id, JSON.stringify({ id: createId(), type: 'NEW_TREE', data: { id: parsed.data.id, x: parsed.data.x } }))
peer.send(JSON.stringify({ id: createId(), type: 'NEW_TREE', data: { id: parsed.data.id, x: parsed.data.x } }))
}
// if (parsed.type === 'NEW_TREE') {
// const room = activeRooms.find((room) => room.peers.find((id) => id === peer.id))
// if (!room) {
// return
// }
// peer.publish(room.id, JSON.stringify({ id: createId(), type: 'NEW_TREE', data: { id: parsed.data.id, x: parsed.data.x } }))
// peer.send(JSON.stringify({ id: createId(), type: 'NEW_TREE', data: { id: parsed.data.id, x: parsed.data.x } }))
// }
}
},

Expand Down
14 changes: 14 additions & 0 deletions apps/website/server/core/rooms/addon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BaseRoom } from './base'

interface AddonRoomOptions {
id: string
token: string
}

export class AddonRoom extends BaseRoom {
peers: string[] = []

constructor({ id, token }: AddonRoomOptions) {
super({ id, token, type: 'ADDON' })
}
}
42 changes: 42 additions & 0 deletions apps/website/server/core/rooms/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Peer } from 'crossws'
import type { Room } from '~~/types/room'
import { createId } from '@paralleldrive/cuid2'

interface BaseRoomOptions {
id: string
token: string
type: Room['type']
}

export class BaseRoom implements Room {
id: string
token: string
type: Room['type']
server: { ws: WebSocket, peer: Peer | null }

constructor({ id, token, type }: BaseRoomOptions) {
this.id = id
this.token = token
this.type = type

const { public: publicEnv } = useRuntimeConfig()

this.server = {
ws: new WebSocket(publicEnv.websocketUrl),
peer: null,
}

this.server.ws.onopen = () => {
const prepearedMessage = JSON.stringify({
id: createId(),
type: 'CONNECT',
data: {
client: 'SERVER',
id: this.id,
token: this.token,
},
})
this.server.ws.send(prepearedMessage)
}
}
}
61 changes: 0 additions & 61 deletions apps/website/server/utils/room.ts

This file was deleted.

11 changes: 11 additions & 0 deletions apps/website/types/room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Peer } from 'crossws'

export interface Room {
id: string
type: 'ADDON' | 'GAME'
token: string
server: {
ws: WebSocket
peer: Peer | null
}
}

0 comments on commit 97b51a9

Please sign in to comment.