diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2025-05-24 15:34:24 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2025-05-24 15:34:24 +0200 |
commit | 339754d960eba6529bebac44e046b4bc799a9e27 (patch) | |
tree | 84acca631b3d1c26fa5cd7d617899473b66f253d | |
parent | e119d1fa06a92aea8f8c0af90b5af816690823fe (diff) |
Throw exception on invalid websocket state
-rw-r--r-- | src/WebSocket.hs | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/WebSocket.hs b/src/WebSocket.hs index 68feae8..4355ff5 100644 --- a/src/WebSocket.hs +++ b/src/WebSocket.hs @@ -65,7 +65,9 @@ startClient addr port path fun = do sendMessage :: Connection -> ByteString -> IO () sendMessage Connection {..} bs = do unsafeUseAsCStringLen bs $ \( ptr, len ) -> do - js_send connJS (castPtr ptr) len + js_send connJS (castPtr ptr) len >>= \case + 0 -> return () + 1 -> error "websocket is not open" receiveMessage :: Connection -> IO ByteString receiveMessage Connection {..} = do @@ -75,8 +77,8 @@ receiveMessage Connection {..} = do foreign import javascript unsafe "const ws = new WebSocket($1); ws.binaryType = 'arraybuffer'; return ws" js_initWebSocket :: JSString -> IO JSVal -foreign import javascript unsafe "$1.send(new Uint8Array(globalThis.wasi_memory.buffer, $2, $3))" - js_send :: JSVal -> Ptr Word8 -> Int -> IO () +foreign import javascript unsafe "if ($1.readyState == WebSocket.OPEN) { $1.send(new Uint8Array(globalThis.wasi_memory.buffer, $2, $3)); return 0; } else { return 1; }" + js_send :: JSVal -> Ptr Word8 -> Int -> IO Int foreign import javascript unsafe "$1.data" js_get_data :: JSVal -> IO JSVal |