Skip to content

Commit

Permalink
feat: add websocket demo to koa
Browse files Browse the repository at this point in the history
  • Loading branch information
GrinZero committed Sep 13, 2024
1 parent 0f226f8 commit 05ebf81
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apps/koa/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const WebSocket = require('ws')

const run = () => {
let register
try {
Expand Down Expand Up @@ -70,6 +72,20 @@ const run = () => {
ctx.body = data
})

router.get('/ws', async (ctx) => {
const ws = new WebSocket('wss://echo.websocket.org/')
ws.onopen = () => {
ws.send('Hello from Koa')
}
ws.onmessage = (event) => {
console.log('WebSocket message:', event.data)
}
ws.onclose = () => {
console.log('WebSocket connection closed')
}
ctx.body = 'WebSocket connection established'
})

app.use(router.routes())
app.listen(3000)

Expand Down

0 comments on commit 05ebf81

Please sign in to comment.