/
gun.gleam
43 lines (32 loc) · 967 Bytes
/
gun.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//// Low level bindings to the gun API. You typically do not need to use this.
//// Prefer the other modules in this library.
import gleam/http.{Header}
import gleam/erlang/charlist.{Charlist}
import gleam/dynamic.{Dynamic}
pub external type StreamReference
pub external type ConnectionPid
pub fn open(host: String, port: Int) -> Result(ConnectionPid, Dynamic) {
open_erl(charlist.from_string(host), port)
}
pub external fn open_erl(Charlist, Int) -> Result(ConnectionPid, Dynamic) =
"gun" "open"
pub external fn await_up(ConnectionPid) -> Result(Dynamic, Dynamic) =
"gun" "await_up"
pub external fn ws_upgrade(
ConnectionPid,
String,
List(Header),
) -> StreamReference =
"gun" "ws_upgrade"
pub type Frame {
Close
Text(String)
Binary(BitString)
}
external type OkAtom
external fn ws_send_erl(ConnectionPid, Frame) -> OkAtom =
"gun" "ws_send"
pub fn ws_send(pid: ConnectionPid, frame: Frame) -> Nil {
ws_send_erl(pid, frame)
Nil
}