ryo-ma / deno-websocket

🦕 A simple WebSocket library like ws of node.js library for deno
https://deno.land/x/websocket
MIT License
152 stars 19 forks source link

Example snippets in readme throws #2

Closed brandonkal closed 4 years ago

brandonkal commented 4 years ago
error: Uncaught TypeError: Cannot read property 'send' of undefined
    return this.webSocket!.send(message);
                           ^
    at WebSocket.send (websocket.ts:96:28)
    at client.ts:10:4

Removing the send line:

error: Uncaught Error: ws: server didn't accept handshake: version=HTTP/1.1, statusCode=302
    throw new Error(
          ^
    at handshake (mod.ts:495:11)
    at async connectWebSocket (mod.ts:539:5)
    at async WebSocket.createSocket (websocket.ts:57:23)
ryo-ma commented 4 years ago

Let me know about your environment. Deno version, OS, using commands, etc.

brandonkal commented 4 years ago

@ryo-ma It was the k3s service-lb. It appears kubernetes is not notifying the underlying host (ubuntu bionic) that it is using a port (8080 in this case). So Deno was happy to bind to that port.

I'm not sure if there is any way to fix that. So the second error above is fine (it's unexpected).

I'm going to leave this open though because the program shouldn't be throwing a TypeError. The issue is that https://github.com/ryo-ma/deno-websocket/blob/master/lib/websocket.ts#L53 is async. The code needs to wait for that to resolve before it accepts messages.

ryo-ma commented 4 years ago

Thank you for your reply. OK. I think too the program shouldn't be throwing a TypeError. I will be throwing the other error when never finished initializing the connection.

ryo-ma commented 4 years ago

I have fixed the error type and error message. https://github.com/ryo-ma/deno-websocket/blob/10a1954ac81c3d09f4bb2269c1b03edf500c5c09/lib/websocket.ts#L114