-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathecho.exs
More file actions
37 lines (29 loc) · 1.51 KB
/
echo.exs
File metadata and controls
37 lines (29 loc) · 1.51 KB
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
Mix.install([:mint_web_socket, :castore])
require Logger
# see https://websocket.org/echo.html
{:ok, conn} = Mint.HTTP.connect(:https, "echo.websocket.org", 443)
Logger.debug("Connected to https://echo.websocket.org:443")
Logger.debug("Upgrading to WebSocket protocol on /")
{:ok, conn, ref} = Mint.WebSocket.upgrade(:wss, conn, "/", [])
message = receive(do: (message -> message))
{:ok, conn, [{:status, ^ref, status}, {:headers, ^ref, resp_headers}, {:done, ^ref}]} =
Mint.WebSocket.stream(conn, message)
{:ok, conn, websocket} = Mint.WebSocket.new(conn, ref, status, resp_headers)
Logger.debug("WebSocket established")
frame = {:text, "Rock it with Mint.WebSocket"}
Logger.debug("Sending frame #{inspect(frame)}")
{:ok, websocket, data} = Mint.WebSocket.encode(websocket, frame)
{:ok, conn} = Mint.WebSocket.stream_request_body(conn, ref, data)
message = receive(do: (message -> message))
{:ok, conn, [{:data, ^ref, data}]} = Mint.WebSocket.stream(conn, message)
{:ok, websocket, frames} = Mint.WebSocket.decode(websocket, data)
Logger.debug("Received frames #{inspect(frames)}")
frame = :close
Logger.debug("Sending frame #{inspect(frame)}")
{:ok, websocket, data} = Mint.WebSocket.encode(websocket, frame)
{:ok, conn} = Mint.WebSocket.stream_request_body(conn, ref, data)
message = receive(do: (message -> message))
{:ok, conn, [{:data, ^ref, data}]} = Mint.WebSocket.stream(conn, message)
{:ok, websocket, frames} = Mint.WebSocket.decode(websocket, data)
Logger.debug("Received frames #{inspect(frames)}")
Mint.HTTP.close(conn)