Add websocket server, will be used soon
This commit is contained in:
27
src/index.ts
27
src/index.ts
@@ -1,5 +1,6 @@
|
|||||||
import { Elysia, t } from 'elysia'
|
import { Elysia, t } from 'elysia'
|
||||||
import { cors } from '@elysiajs/cors'
|
import { cors } from '@elysiajs/cors'
|
||||||
|
import { ElysiaWS } from 'elysia/dist/ws'
|
||||||
import { jsonResponse } from './lib/util'
|
import { jsonResponse } from './lib/util'
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import swagger from '@elysiajs/swagger'
|
import swagger from '@elysiajs/swagger'
|
||||||
@@ -48,7 +49,12 @@ const boolNotStr = (name: string) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = new Elysia({ prefix: '/api' })
|
const app = new Elysia({
|
||||||
|
prefix: '/api',
|
||||||
|
websocket: {
|
||||||
|
idleTimeout: 10
|
||||||
|
}
|
||||||
|
})
|
||||||
.use(
|
.use(
|
||||||
cors({
|
cors({
|
||||||
origin: '*',
|
origin: '*',
|
||||||
@@ -69,6 +75,25 @@ const app = new Elysia({ prefix: '/api' })
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const clients = new Set<ElysiaWS>()
|
||||||
|
|
||||||
|
app.ws('/ws', {
|
||||||
|
open (ws) {
|
||||||
|
clients.add(ws)
|
||||||
|
console.log(ws.id, 'connected')
|
||||||
|
},
|
||||||
|
message (ws, message) {
|
||||||
|
console.log('received:', message, 'from', ws.id)
|
||||||
|
},
|
||||||
|
close (ws) {
|
||||||
|
clients.forEach(client => {
|
||||||
|
if (client.id === ws.id) clients.delete(client)
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(ws.id, 'disconnected')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
app.post('/get-verify-code', context => getVerifyCodeHandler(context), {
|
app.post('/get-verify-code', context => getVerifyCodeHandler(context), {
|
||||||
detail: {
|
detail: {
|
||||||
hide: true //This endpoint can only be used by the website.
|
hide: true //This endpoint can only be used by the website.
|
||||||
|
|||||||
Reference in New Issue
Block a user