@@ -82,7 +82,8 @@ const io = new Server<
82
82
83
83
### With oak
84
84
85
- You need to use the [ .handle()] ( https://github.com/oakserver/oak#handle-method ) method:
85
+ You need to use the [ .handle()] ( https://github.com/oakserver/oak#handle-method )
86
+ method:
86
87
87
88
``` ts
88
89
import { serve } from " https://deno.land/std@0.166.0/http/server.ts" ;
@@ -92,32 +93,27 @@ import { Application } from "https://deno.land/x/oak@v11.1.0/mod.ts";
92
93
const app = new Application ();
93
94
94
95
app .use ((ctx ) => {
95
- ctx .response .body = " Hello World!" ;
96
+ ctx .response .body = " Hello World!" ;
96
97
});
97
98
98
99
const io = new Server ();
99
100
100
101
io .on (" connection" , (socket ) => {
101
- console .log (` socket ${socket .id } connected ` );
102
+ console .log (` socket ${socket .id } connected ` );
102
103
103
- socket .emit (" hello" , " world" );
104
+ socket .emit (" hello" , " world" );
104
105
105
- socket .on (" disconnect" , (reason ) => {
106
- console .log (` socket ${socket .id } disconnected due to ${reason } ` );
107
- });
106
+ socket .on (" disconnect" , (reason ) => {
107
+ console .log (` socket ${socket .id } disconnected due to ${reason } ` );
108
+ });
108
109
});
109
110
110
111
const handler = io .handler (async (req ) => {
111
- const response = await app .handle (req );
112
- if (response ) {
113
- return response ;
114
- } else {
115
- return new Response (null , { status: 404 });
116
- }
112
+ return await app .handle (req ) || new Response (null , { status: 404 });
117
113
});
118
114
119
115
await serve (handler , {
120
- port: 3000 ,
116
+ port: 3000 ,
121
117
});
122
118
```
123
119
0 commit comments