ryo-ma/deno-websocket

Example snippets in readme throws

brandonkal opened this issue · 4 comments

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)

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

@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.

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.

I have fixed the error type and error message.

throw new WebSocketError(