Allow configuring server port
This commit is contained in:
parent
6abf787d9c
commit
bc9ef895ad
1 changed files with 51 additions and 47 deletions
112
server/index.ts
112
server/index.ts
|
|
@ -9,60 +9,64 @@ type Message =
|
|||
|
||||
const clients: Map<WebsocketData['clientId'], WebSocket> = new Map()
|
||||
|
||||
const _server = Deno.serve((req) => {
|
||||
const _server = Deno.serve({
|
||||
port: Number.parseInt(Deno.env.get('PORT') || '8000', 10),
|
||||
handler: (req) => {
|
||||
if (req.headers.get("upgrade") !== "websocket") {
|
||||
return new Response(null, { status: 501 })
|
||||
return new Response(null, { status: 501 })
|
||||
}
|
||||
|
||||
const { socket, response } = Deno.upgradeWebSocket(req)
|
||||
|
||||
// TODO Allow creating private ponds
|
||||
const clientId = crypto.randomUUID()
|
||||
console.log('generated clientId', clientId)
|
||||
|
||||
socket.addEventListener("open", () => {
|
||||
// register newly connected client and tell them how many other people are there
|
||||
console.log('Connection opened', clientId, clients)
|
||||
socket.send(JSON.stringify(<Message>{
|
||||
type: 'welcome',
|
||||
id: clientId
|
||||
}))
|
||||
const enterNotice = JSON.stringify(<Message>{
|
||||
type: 'presence-information',
|
||||
others: clients.size,
|
||||
})
|
||||
clients.set(clientId, socket)
|
||||
for (const client of clients.values()) {
|
||||
client.send(enterNotice)
|
||||
}
|
||||
})
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
const message = `${event.data}`
|
||||
// broadcast message to all other clients
|
||||
// TODO: Validate message shape
|
||||
const msg = JSON.parse(`${message}`)
|
||||
console.log('Relaying message from', clientId, msg)
|
||||
for (const [uuid, client] of clients.entries()) {
|
||||
if (uuid !== clientId) {
|
||||
client.send(message)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
socket.addEventListener("close", () => {
|
||||
// remove client from list of registered clients and tell other clients how many people are there
|
||||
console.log('Connection closed', clientId, clients)
|
||||
clients.delete(clientId)
|
||||
const leaveNotice = JSON.stringify(<Message>{
|
||||
type: 'presence-information',
|
||||
others: clients.size - 1,
|
||||
})
|
||||
for (const [uuid, client] of clients.entries()) {
|
||||
if (uuid !== clientId) {
|
||||
client.send(leaveNotice)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
const { socket, response } = Deno.upgradeWebSocket(req)
|
||||
// TODO Allow creating private ponds
|
||||
const clientId = crypto.randomUUID()
|
||||
console.log('generated clientId', clientId)
|
||||
|
||||
socket.addEventListener("open", () => {
|
||||
// register newly connected client and tell them how many other people are there
|
||||
console.log('Connection opened', clientId, clients)
|
||||
socket.send(JSON.stringify(<Message>{
|
||||
type: 'welcome',
|
||||
id: clientId
|
||||
}))
|
||||
const enterNotice = JSON.stringify(<Message>{
|
||||
type: 'presence-information',
|
||||
others: clients.size,
|
||||
})
|
||||
clients.set(clientId, socket)
|
||||
for (const client of clients.values()) {
|
||||
client.send(enterNotice)
|
||||
}
|
||||
})
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
const message = `${event.data}`
|
||||
// broadcast message to all other clients
|
||||
// TODO: Validate message shape
|
||||
const msg = JSON.parse(`${message}`)
|
||||
console.log('Relaying message from', clientId, msg)
|
||||
for (const [uuid, client] of clients.entries()) {
|
||||
if (uuid !== clientId) {
|
||||
client.send(message)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
socket.addEventListener("close", () => {
|
||||
// remove client from list of registered clients and tell other clients how many people are there
|
||||
console.log('Connection closed', clientId, clients)
|
||||
clients.delete(clientId)
|
||||
const leaveNotice = JSON.stringify(<Message>{
|
||||
type: 'presence-information',
|
||||
others: clients.size - 1,
|
||||
})
|
||||
for (const [uuid, client] of clients.entries()) {
|
||||
if (uuid !== clientId) {
|
||||
client.send(leaveNotice)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return response
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue