Skip to content

Commit 8df08ff

Browse files
authored
Update to latest std lib, return socket from handleUpgrade method (#12)
* Update std lib to v0.89.0 for modern compatibility * Return socket from handleUpgrade method For compatibility with server frameworks such as abc.
1 parent fa1745d commit 8df08ff

5 files changed

Lines changed: 25 additions & 24 deletions

File tree

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ A WebSocket server library for <a href="https://deno.land">Deno</a>.
1212
The [raison d'être](https://en.wiktionary.org/wiki/raison_d%27%C3%AAtre) for this library is to provide a unified async iterator for the events of all connected WebSocket clients.
1313

1414
**Note**: This WebSocket server is **not** an `EventEmitter` (i.e. it does not use events with callbacks like [websockets/ws](https://github.com/websockets/ws)).
15-
Instead, it specifies the [asyncIterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator) symbol and should be used in conjunction with a [`for await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) loop, just like the [Deno http server](https://deno.land/std@0.53.0/http/server.ts).
15+
Instead, it specifies the [asyncIterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator) symbol and should be used in conjunction with a [`for await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) loop, just like the [Deno http server](https://deno.land/std@0.89.0/http/server.ts).
1616
The iterator return values are of
1717
```typescript
1818
type WebSocketServerEvent = {
@@ -44,7 +44,7 @@ listenAndServe(":8080", ({ socket, event }) => {
4444

4545
### Using an existing HTTP server
4646
```typescript
47-
import { serve } from "https://deno.land/std@0.53.0/http/server.ts";
47+
import { serve } from "https://deno.land/std@0.89.0/http/server.ts";
4848
import { WebSocketServer } from "https://deno.land/x/websocket_server/mod.ts";
4949

5050
const httpServer = serve(":8080");
@@ -59,7 +59,7 @@ for await (const { event, socket } of wss) {
5959

6060
### Multiple WebSocket servers sharing an existing HTTP server
6161
```typescript
62-
import { serve } from "https://deno.land/std@0.53.0/http/server.ts";
62+
import { serve } from "https://deno.land/std@0.89.0/http/server.ts";
6363
import { WebSocketServer } from "https://deno.land/x/websocket_server/mod.ts";
6464

6565
async function serverHandler(wss: WebSocketServer, message: string) {
@@ -99,13 +99,12 @@ Check out the example [echo/broadcast server](example_server.ts).
9999
## FAQ
100100

101101
### How do I create a WebSocket client?
102-
This library provides a class only for WebSocket servers, not WebSocket clients, because it is straightforward to create clients with the [std/ws](https://deno.land/std@0.53.0/ws/) module.
102+
This library provides a class only for WebSocket servers, not WebSocket clients, because it is straightforward to create clients with the [WebSocket API](https://developer.mozilla.org/docs/Web/API/WebSockets_API).
103103

104104
Here is a simple example:
105105
```typescript
106-
import { connectWebSocket } from "https://deno.land/std@0.53.0/ws/mod.ts";
107106
try {
108-
const socket = await connectWebSocket("ws://127.0.0.1:8080");
107+
const socket = new WebSocket("ws://127.0.0.1:8080");
109108
for await (const event of socket) {
110109
console.log(event);
111110
if (typeof event === "string" && event === "Who is this?") {

mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export {
33
isWebSocketPingEvent,
44
isWebSocketPongEvent,
55
isWebSocketCloseEvent,
6-
} from "https://deno.land/std@0.53.0/ws/mod.ts";
6+
} from "https://deno.land/std@0.89.0/ws/mod.ts";

queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deferred, Deferred } from "https://deno.land/std@0.53.0/async/mod.ts";
1+
import { deferred, Deferred } from "https://deno.land/std@0.89.0/async/mod.ts";
22

33
export class Queue<T> {
44
private stopSignal: boolean;

server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import {
55
ServerRequest,
66
HTTPOptions,
77
HTTPSOptions,
8-
} from "https://deno.land/std@0.53.0/http/server.ts";
8+
} from "https://deno.land/std@0.89.0/http/server.ts";
99
import { Queue } from "./queue.ts";
1010
import {
1111
acceptWebSocket,
1212
WebSocket,
1313
WebSocketEvent,
14-
} from "https://deno.land/std@0.53.0/ws/mod.ts";
14+
} from "https://deno.land/std@0.89.0/ws/mod.ts";
1515

1616
type WebSocketServerEvent = {
1717
event: WebSocketEvent;
@@ -70,6 +70,7 @@ export class WebSocketServer {
7070
);
7171
this.trackSocket(socket);
7272
this.handleSocketEvents(socket);
73+
return socket;
7374
} catch (err) {
7475
console.error(err);
7576
await req.respond({

server_test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {
22
assertEquals, assertNotEquals,
3-
} from "https://deno.land/std@0.53.0/testing/asserts.ts";
4-
import { delay } from "https://deno.land/std@0.53.0/async/delay.ts";
3+
} from "https://deno.land/std@0.89.0/testing/asserts.ts";
4+
import { delay } from "https://deno.land/std@0.89.0/async/delay.ts";
55
import {
6-
connectWebSocket,
76
WebSocket,
87
WebSocketEvent,
9-
} from "https://deno.land/std@0.53.0/ws/mod.ts";
8+
} from "https://deno.land/std@0.89.0/ws/mod.ts";
109
import { serve, WebSocketServer } from "./server.ts";
1110

1211
const { test } = Deno;
@@ -35,19 +34,21 @@ test("that a `WebSocketServer` receives events from a client", async () => {
3534
saveIterationResults(results, server);
3635

3736
const msgsPerClient = 3;
38-
const client1 = await connectWebSocket(`ws://127.0.0.1:${port}`);
39-
const client2 = await connectWebSocket(`ws://127.0.0.1:${port}`);
40-
await client1.send("client 1");
37+
const client1 = new WebSocket(`ws://127.0.0.1:${port}`);
38+
await new Promise(resolve => client1.addEventListener('open', resolve))
39+
const client2 = new WebSocket(`ws://127.0.0.1:${port}`);
40+
await new Promise(resolve => client2.addEventListener('open', resolve))
41+
client1.send("client 1");
4142
await delay(10);
42-
await client2.send("client 2");
43+
client2.send("client 2");
4344
await delay(10);
44-
await client1.send(new Uint8Array([1, 2, 3]));
45+
client1.send(new Uint8Array([1, 2, 3]));
4546
await delay(10);
46-
await client2.send(new Uint8Array([4, 5, 6]));
47+
client2.send(new Uint8Array([4, 5, 6]));
4748
await delay(10);
48-
await client1.close(1001, "done");
49+
client1.close(3001, "done");
4950
await delay(10);
50-
await client2.close(1002, "done");
51+
client2.close(3002, "done");
5152
await delay(10);
5253

5354
assertEquals(server.sockets.size, 0, "sockets have not been untracked");
@@ -63,8 +64,8 @@ test("that a `WebSocketServer` receives events from a client", async () => {
6364
assertEquals(results[1].event, "client 2");
6465
assertEquals(results[2].event, new Uint8Array([1, 2, 3]));
6566
assertEquals(results[3].event, new Uint8Array([4, 5, 6]));
66-
assertEquals(results[4].event, { code: 1001, reason: "done" });
67-
assertEquals(results[5].event, { code: 1002, reason: "done" });
67+
assertEquals(results[4].event, { code: 3001, reason: "done" });
68+
assertEquals(results[5].event, { code: 3002, reason: "done" });
6869

6970
await server.close();
7071
});

0 commit comments

Comments
 (0)