| Copyright | (C) 2016-2025 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.WebSocket
Description
Synopsis
- connect :: FromJSON json => URL -> (WebSocket -> action) -> (Closed -> action) -> (Payload json -> action) -> (MisoString -> action) -> Effect parent model action
- connectJSON :: FromJSON json => URL -> (WebSocket -> action) -> (Closed -> action) -> (json -> action) -> (MisoString -> action) -> Effect parent model action
- connectText :: URL -> (WebSocket -> action) -> (Closed -> action) -> (MisoString -> action) -> (MisoString -> action) -> Effect parent model action
- connectBLOB :: URL -> (WebSocket -> action) -> (Closed -> action) -> (Blob -> action) -> (MisoString -> action) -> Effect parent model action
- connectArrayBuffer :: URL -> (WebSocket -> action) -> (Closed -> action) -> (ArrayBuffer -> action) -> (MisoString -> action) -> Effect parent model action
- sendText :: WebSocket -> MisoString -> Effect parent model action
- sendJSON :: ToJSON json => WebSocket -> json -> Effect parent model action
- sendBLOB :: WebSocket -> Blob -> Effect parent model action
- sendArrayBuffer :: WebSocket -> ArrayBuffer -> Effect parent model action
- close :: WebSocket -> Effect parent model action
- socketState :: WebSocket -> (SocketState -> action) -> Effect parent model action
- emptyWebSocket :: WebSocket
- newtype WebSocket = WebSocket Int
- type URL = MisoString
- data SocketState
- = CONNECTING
- | OPEN
- | CLOSING
- | CLOSED
- data CloseCode
- data Closed = Closed {
- closedCode :: CloseCode
- wasClean :: Bool
- reason :: MisoString
- data Payload value
- = JSON value
- | BLOB Blob
- | TEXT MisoString
- | BUFFER ArrayBuffer
- newtype Blob = Blob JSVal
- newtype ArrayBuffer = ArrayBuffer JSVal
WebSocket
Arguments
| :: FromJSON json | |
| => URL | URL endpoint for a |
| -> (WebSocket -> action) |
|
| -> (Closed -> action) |
|
| -> (Payload json -> action) |
|
| -> (MisoString -> action) | Error message callback |
| -> Effect parent model action |
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/connect
Establishes a WebSocket server that receives potentially multiple different Payload.
It's more common to use connectJSON or connectText. But connect can be used to received multiple
different kinds of data from a WebSocket server.
Arguments
| :: FromJSON json | |
| => URL | URL endpoint for a |
| -> (WebSocket -> action) |
|
| -> (Closed -> action) |
|
| -> (json -> action) |
|
| -> (MisoString -> action) | Error message callback |
| -> Effect parent model action |
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/connect
Establishes a WebSocket server that assumes a JSON-encoded protocol.
Arguments
| :: URL | URL endpoint for a |
| -> (WebSocket -> action) |
|
| -> (Closed -> action) |
|
| -> (MisoString -> action) |
|
| -> (MisoString -> action) | Error message callback |
| -> Effect parent model action |
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/connect
Establishes a WebSocket server that assumes a text-encoded protocol.
Arguments
| :: URL | URL endpoint for a |
| -> (WebSocket -> action) |
|
| -> (Closed -> action) |
|
| -> (Blob -> action) |
|
| -> (MisoString -> action) |
|
| -> Effect parent model action |
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/connect
Establishes a WebSocket server that assumes a binary-encoded protocol.
Arguments
| :: URL | URL endpoint for a |
| -> (WebSocket -> action) |
|
| -> (Closed -> action) |
|
| -> (ArrayBuffer -> action) |
|
| -> (MisoString -> action) |
|
| -> Effect parent model action |
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/connect
Establishes a WebSocket server that assumes an ArrayBuffer protocol.
Arguments
| :: WebSocket |
|
| -> MisoString | A text payload to send to |
| -> Effect parent model action |
Arguments
| :: ToJSON json | |
| => WebSocket |
|
| -> json | A JSON-encoded message |
| -> Effect parent model action |
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
data Person = Person { name :: MisoString, age :: Int }
deriving (Show, Eq)
instance ToJSON Person where
toJSON (Person name age) = object [ "name" .= name, "age" .= age ]
test :: WebSocket -> Effect parent model action
test connection = do
sendJSON (connection :: WebSocket) (Person "alice" 42)
sendJSON (connection :: WebSocket) (Person "bob" 42)
Arguments
| :: WebSocket |
|
| -> ArrayBuffer | An |
| -> Effect parent model action |
Arguments
| :: WebSocket |
|
| -> Effect parent model action |
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close
It is very important to close the WebSocket, otherwise leaks can occur.
close is a no-op if invoked multiple times.
socketState :: WebSocket -> (SocketState -> action) -> Effect parent model action Source #
Defaults
emptyWebSocket :: WebSocket Source #
A null WebSocket is one with a negative descriptor.
Types
Type for holding a WebSocket file descriptor.
type URL = MisoString Source #
URL that the WebSocket will connect to
data SocketState Source #
SocketState corresponding to current WebSocket connection
Constructors
| CONNECTING | 0 |
| OPEN | 1 |
| CLOSING | 2 |
| CLOSED | 3 |
Instances
Code corresponding to a closed connection https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
Constructors
| CLOSE_NORMAL | 1000, Normal closure; the connection successfully completed whatever purpose for which it was created. |
| CLOSE_GOING_AWAY | 1001, The endpoint is going away, either because of a server failure or because the browser is navigating away from the page that opened the connection. |
| CLOSE_PROTOCOL_ERROR | 1002, The endpoint is terminating the connection due to a protocol error. |
| CLOSE_UNSUPPORTED | 1003, The connection is being terminated because the endpoint received data of a type it cannot accept (for example, a textonly endpoint received binary data). |
| CLOSE_NO_STATUS | 1005, Reserved. Indicates that no status code was provided even though one was expected. |
| CLOSE_ABNORMAL | 1006, Reserved. Used to indicate that a connection was closed abnormally (that is, with no close frame being sent) when a status code is expected. |
| Unsupported_Data | 1007, The endpoint is terminating the connection because a message was received that contained inconsistent data (e.g., nonUTF8 data within a text message). |
| Policy_Violation | 1008, The endpoint is terminating the connection because it received a message that violates its policy. This is a generic status code, used when codes 1003 and 1009 are not suitable. |
| CLOSE_TOO_LARGE | 1009, The endpoint is terminating the connection because a data frame was received that is too large. |
| Missing_Extension | 1010, The client is terminating the connection because it expected the server to negotiate one or more extension, but the server didn't. |
| Internal_Error | 1011, The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request. |
| Service_Restart | 1012, The server is terminating the connection because it is restarting. |
| Try_Again_Later | 1013, The server is terminating the connection due to a temporary condition, e.g. it is overloaded and is casting off some of its clients. |
| TLS_Handshake | 1015, Reserved. Indicates that the connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified). |
| OtherCode Int | OtherCode that is reserved and not in the range 0999 |
Instances
Closed message is sent when a WebSocket has closed
Constructors
| Closed | |
Fields
| |
Payload is used as the potential source of data when working with EventSource
Constructors
| JSON value | JSON-encoded data |
| BLOB Blob | Binary encoded data |
| TEXT MisoString | Text encoded data |
| BUFFER ArrayBuffer | Buffered data |
newtype ArrayBuffer Source #
Constructors
| ArrayBuffer JSVal |
Instances
| FromJSVal ArrayBuffer Source # | |
Defined in Miso.FFI.Internal Methods fromJSVal :: JSVal -> JSM (Maybe ArrayBuffer) Source # fromJSValUnchecked :: JSVal -> JSM ArrayBuffer Source # fromJSValListOf :: JSVal -> JSM (Maybe [ArrayBuffer]) Source # fromJSValUncheckedListOf :: JSVal -> JSM [ArrayBuffer] Source # | |
| ToJSVal ArrayBuffer Source # | |
Defined in Miso.FFI.Internal Methods toJSVal :: ArrayBuffer -> JSM JSVal Source # toJSValListOf :: [ArrayBuffer] -> JSM JSVal Source # | |