Skip to content

Commit

Permalink
feat: add support for websocket handler
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Jan 22, 2025
1 parent 44b06c6 commit a22fbd6
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 82 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@adonisjs/hash": "^9.0.5",
"@nuxt/kit": "^3.15.2",
"defu": "^6.1.4",
"h3": "^1.13.1",
"h3": "^1.14.0",
"hookable": "^5.5.3",
"ofetch": "^1.4.1",
"ohash": "^1.1.4",
Expand Down
7 changes: 7 additions & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ const providers = computed(() =>
external: true,
})),
)
onMounted(() => {
const ws = new WebSocket('/ws')
ws.addEventListener('message', (event) => {
console.log('message', event.data)
})
})
</script>

<template>
Expand Down
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineNuxtConfig({
nitro: {
experimental: {
database: true,
websocket: true,
},
},
auth: {
Expand Down
3 changes: 3 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
},
"devDependencies": {
"better-sqlite3": "^11.8.1"
},
"resolutions": {
"h3": "^1.14.0"
}
}
15 changes: 15 additions & 0 deletions playground/server/routes/ws/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default defineWebSocketHandler({
// async upgrade(request) {
// const session = await getUserSession(request)
// console.log(`[upgrade] Session id: ${session.id}`)
// return {}
// },
async open(peer) {

Check failure on line 7 in playground/server/routes/ws/index.ts

View workflow job for this annotation

GitHub Actions / lint

'peer' is defined but never used. Allowed unused args must match /^_/u
// const session = await getUserSession(peer.request)
// console.log(`[open] Session:`, session)
// peer.send(`Hello, ${session.id}!`)
},
message(peer, message) {

Check failure on line 12 in playground/server/routes/ws/index.ts

View workflow job for this annotation

GitHub Actions / lint

'peer' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 12 in playground/server/routes/ws/index.ts

View workflow job for this annotation

GitHub Actions / lint

'message' is defined but never used. Allowed unused args must match /^_/u
// console.log('message', message)
},
})
Loading

0 comments on commit a22fbd6

Please sign in to comment.