Skip to content

Commit

Permalink
docs: add example with oak (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Nov 29, 2022
1 parent ae9f331 commit 7f47539
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/std@0.166.0/http/server.ts";
Expand All @@ -92,32 +93,27 @@ import { Application } from "https://deno.land/x/oak@v11.1.0/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,
});
```

Expand Down

0 comments on commit 7f47539

Please sign in to comment.