Skip to content

Commit

Permalink
docs: add example with oak
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Nov 29, 2022
1 parent 02b0aea commit ae9f331
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ An implementation of the Socket.IO protocol for Deno.
Table of content:

- [Usage](#usage)
- [With oak](#with-oak)
- [Options](#options)
- [`path`](#path)
- [`connectTimeout`](#connecttimeout)
Expand All @@ -23,7 +24,7 @@ Table of content:
## Usage

```ts
import { serve } from "https://deno.land/std@0.150.0/http/server.ts";
import { serve } from "https://deno.land/std@0.166.0/http/server.ts";
import { Server } from "https://deno.land/x/[email protected]/mod.ts";

const io = new Server();
Expand Down Expand Up @@ -79,6 +80,47 @@ const io = new Server<
>();
```

### With oak

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";
import { Server } from "https://deno.land/x/[email protected]/mod.ts";
import { Application } from "https://deno.land/x/[email protected]/mod.ts";

const app = new Application();

app.use((ctx) => {
ctx.response.body = "Hello World!";
});

const io = new Server();

io.on("connection", (socket) => {
console.log(`socket ${socket.id} connected`);

socket.emit("hello", "world");

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 });
}
});

await serve(handler, {
port: 3000,
});
```

## Options

### `path`
Expand Down Expand Up @@ -214,7 +256,7 @@ The library relies on the standard `log` module, so you can display the internal
logs of the Socket.IO server with:

```ts
import * as log from "https://deno.land/std@0.150.0/log/mod.ts";
import * as log from "https://deno.land/std@0.166.0/log/mod.ts";

await log.setup({
handlers: {
Expand Down

0 comments on commit ae9f331

Please sign in to comment.