|
| 1 | +open Js.TypedArray2 |
| 2 | + |
| 3 | +type readyState |
| 4 | +type t<'binaryType> = private { |
| 5 | + binaryType: [#blob | #arraybuffer], |
| 6 | + bufferedAmount: float, |
| 7 | + extensions: string, |
| 8 | + protocol: string, |
| 9 | + readyState: readyState, |
| 10 | + url: string, |
| 11 | +} |
| 12 | +@new external make: string => t<Webapi__Blob.t> = "WebSocket" |
| 13 | +@new external makeArrayBuffer: string => t<array_buffer> = "WebSocket" |
| 14 | +@set external setArrayBuffer: (t<array_buffer>, @as("arraybuffer") _) => unit = "binaryType" |
| 15 | +let makeArrayBuffer = url => { |
| 16 | + let ws = makeArrayBuffer(url) |
| 17 | + ws->setArrayBuffer |
| 18 | + ws |
| 19 | +} |
| 20 | + |
| 21 | +@val @scope("WebSocket") external readyStateClosing: readyState = "CLOSING" |
| 22 | +@val @scope("WebSocket") external readyStateClosed: readyState = "CLOSED" |
| 23 | +@val @scope("WebSocket") external readyStateConnected: readyState = "CONNECTING" |
| 24 | +@val @scope("WebSocket") external readyStateOpen: readyState = "OPEN" |
| 25 | + |
| 26 | +let isOpen = ws => ws.readyState === readyStateOpen |
| 27 | + |
| 28 | +@send external close: t<'binaryType> => unit = "close" |
| 29 | +@send external closeWithReason: (t<'binaryType>, ~code: int, ~reason: string) => unit = "close" |
| 30 | + |
| 31 | +@send |
| 32 | +external addOpenListener: (t<'binaryType>, @as("open") _, Webapi__Dom__Event.t => unit) => unit = |
| 33 | + "addEventListener" |
| 34 | +@send |
| 35 | +external removeOpenListener: (t<'binaryType>, @as("open") _, Webapi__Dom__Event.t => unit) => unit = |
| 36 | + "removeEventListener" |
| 37 | + |
| 38 | +type closeEvent = private { |
| 39 | + code: int, |
| 40 | + reason: string, |
| 41 | + wasClean: bool, |
| 42 | +} |
| 43 | +@send |
| 44 | +external addCloseListener: (t<'binaryType>, @as("close") _, closeEvent => unit) => unit = |
| 45 | + "addEventListener" |
| 46 | +@send |
| 47 | +external removeCloseListener: (t<'binaryType>, @as("close") _, closeEvent => unit) => unit = |
| 48 | + "removeEventListener" |
| 49 | + |
| 50 | +@send |
| 51 | +external addErrorListener: (t<'binaryType>, @as("error") _, Webapi__Dom__Event.t => unit) => unit = |
| 52 | + "addEventListener" |
| 53 | +@send |
| 54 | +external removeErrorListener: ( |
| 55 | + t<'binaryType>, |
| 56 | + @as("error") _, |
| 57 | + Webapi__Dom__Event.t => unit, |
| 58 | +) => unit = "removeEventListener" |
| 59 | + |
| 60 | +type messageEvent<'binaryType> = { |
| 61 | + data: Js.Json.t, |
| 62 | + origin: string, |
| 63 | + lastEventId: string, |
| 64 | +} |
| 65 | +@send |
| 66 | +external addMessageListener: ( |
| 67 | + t<'binaryType>, |
| 68 | + @as("message") _, |
| 69 | + messageEvent<'binaryType> => unit, |
| 70 | +) => unit = "addEventListener" |
| 71 | +@send |
| 72 | +external removeMessageListener: ( |
| 73 | + t<'binaryType>, |
| 74 | + @as("message") _, |
| 75 | + messageEvent<'binaryType> => unit, |
| 76 | +) => unit = "removeEventListener" |
| 77 | + |
| 78 | +@send external sendText: (t<'binaryType>, string) => unit = "send" |
| 79 | +@send external sendBlob: (t<'binaryType>, Webapi__Blob.t) => unit = "send" |
| 80 | +@send external sendArrayBuffer: (t<'binaryType>, array_buffer) => unit = "send" |
| 81 | +@send external sendInt8Array: (t<'binaryType>, Int8Array.t) => unit = "send" |
| 82 | +@send external sendInt16Array: (t<'binaryType>, Int16Array.t) => unit = "send" |
| 83 | +@send external sendInt32Array: (t<'binaryType>, Int32Array.t) => unit = "send" |
| 84 | +@send external sendUint8Array: (t<'binaryType>, Uint8Array.t) => unit = "send" |
| 85 | +@send external sendUint8ClampedArray: (t<'binaryType>, Uint8ClampedArray.t) => unit = "send" |
| 86 | +@send external sendUint16Array: (t<'binaryType>, Uint16Array.t) => unit = "send" |
| 87 | +@send external sendUint32Array: (t<'binaryType>, Uint32Array.t) => unit = "send" |
| 88 | +@send external sendFloat32Array: (t<'binaryType>, Float32Array.t) => unit = "send" |
| 89 | +@send external sendFloat64Array: (t<'binaryType>, Float64Array.t) => unit = "send" |
| 90 | +@send external sendDataView: (t<'binaryType>, DataView.t) => unit = "send" |
| 91 | + |
| 92 | +// the data type for binary messages - blob or arraybuffer - is determined by the `binaryType` property set in `make()`. |
| 93 | +@get external unsafeMessageAsBinary: messageEvent<'binaryType> => 'binaryType = "data" |
| 94 | +@get external unsafeMessageAsText: messageEvent<'binaryType> => string = "data" |
| 95 | +let messageIsText = message => { |
| 96 | + open Js.Types |
| 97 | + test(message.data, String) |
| 98 | +} |
| 99 | + |
| 100 | +let messageAsText = message => { |
| 101 | + if message->messageIsText { |
| 102 | + Some(message->unsafeMessageAsText) |
| 103 | + } else { |
| 104 | + None |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +let messageAsBinary = message => { |
| 109 | + if !(message->messageIsText) { |
| 110 | + Some(message->unsafeMessageAsBinary) |
| 111 | + } else { |
| 112 | + None |
| 113 | + } |
| 114 | +} |
0 commit comments