diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2025-07-16 21:08:29 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2025-07-16 21:08:29 +0200 |
commit | e2575d71622b918387a6d415b2ee20f1bcfd8334 (patch) | |
tree | 87681236495d9c7cd2bc4d8ec096f7dd7d765766 /src | |
parent | 829a46ab5b15fbf5cc1ad7eb6d40ec89f82f837d (diff) |
Close WebSocket connection when peer drops
Diffstat (limited to 'src')
-rw-r--r-- | src/WebSocket.hs | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/WebSocket.hs b/src/WebSocket.hs index 4355ff5..c3e6e84 100644 --- a/src/WebSocket.hs +++ b/src/WebSocket.hs @@ -41,6 +41,7 @@ instance Show Connection where instance PeerAddressType Connection where sendBytesToAddress = sendMessage + connectionToAddressClosed = closeConnection startClient :: String -> Int -> String -> (Connection -> IO ()) -> IO () startClient addr port path fun = do @@ -73,6 +74,10 @@ receiveMessage :: Connection -> IO ByteString receiveMessage Connection {..} = do readChan connInQueue +closeConnection :: Connection -> IO () +closeConnection Connection {..} = do + js_close connJS + foreign import javascript unsafe "const ws = new WebSocket($1); ws.binaryType = 'arraybuffer'; return ws" js_initWebSocket :: JSString -> IO JSVal @@ -80,6 +85,9 @@ foreign import javascript unsafe "const ws = new WebSocket($1); ws.binaryType = 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.close()" + js_close :: JSVal -> IO () + foreign import javascript unsafe "$1.data" js_get_data :: JSVal -> IO JSVal |