-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae9f331
commit 7f47539
Showing
1 changed file
with
10 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,8 @@ const io = new Server< | |
|
||
### With oak | ||
|
||
You need to use the [.handle()](https://github.com/oakserver/oak#handle-method) method: | ||
You need to use the [.handle()](https://github.com/oakserver/oak#handle-method) | ||
method: | ||
|
||
```ts | ||
import { serve } from "https://deno.land/[email protected]/http/server.ts"; | ||
|
@@ -92,32 +93,27 @@ import { Application } from "https://deno.land/x/[email protected]/mod.ts"; | |
const app = new Application(); | ||
|
||
app.use((ctx) => { | ||
ctx.response.body = "Hello World!"; | ||
ctx.response.body = "Hello World!"; | ||
}); | ||
|
||
const io = new Server(); | ||
|
||
io.on("connection", (socket) => { | ||
console.log(`socket ${socket.id} connected`); | ||
console.log(`socket ${socket.id} connected`); | ||
|
||
socket.emit("hello", "world"); | ||
socket.emit("hello", "world"); | ||
|
||
socket.on("disconnect", (reason) => { | ||
console.log(`socket ${socket.id} disconnected due to ${reason}`); | ||
}); | ||
socket.on("disconnect", (reason) => { | ||
console.log(`socket ${socket.id} disconnected due to ${reason}`); | ||
}); | ||
}); | ||
|
||
const handler = io.handler(async (req) => { | ||
const response = await app.handle(req); | ||
if (response) { | ||
return response; | ||
} else { | ||
return new Response(null, { status: 404 }); | ||
} | ||
return await app.handle(req) || new Response(null, { status: 404 }); | ||
}); | ||
|
||
await serve(handler, { | ||
port: 3000, | ||
port: 3000, | ||
}); | ||
``` | ||
|
||
|