Skip to content

Commit

Permalink
add example for websocket.org echo demo
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed Jul 1, 2021
1 parent c027b9a commit 9e758d6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/echo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# see https://websocket.org/echo.html
{:ok, conn} = Mint.HTTP.connect(:https, "echo.websocket.org", 443)

{:ok, conn, ref} = Mint.WebSocket.upgrade(conn, "/", [])

message = receive(do: (message -> message))
{:ok, conn, [{:status, ^ref, status}, {:headers, ^ref, resp_headers}, {:done, ^ref}]} =
Mint.HTTP.stream(conn, message)
{:ok, conn, websocket} = Mint.WebSocket.new(conn, ref, status, resp_headers)

frame = {:text, "Rock it with Mint.WebSocket"}

{:ok, websocket, data} = Mint.WebSocket.encode(websocket, frame)
{:ok, conn} = Mint.HTTP.stream_request_body(conn, ref, data)

message = receive(do: (message -> message))
{:ok, conn, [{:data, ^ref, data}]} = Mint.HTTP.stream(conn, message)
{:ok, websocket, [{:text, "Rock it with Mint.WebSocket"}]} =
Mint.WebSocket.decode(websocket, data)

{:ok, websocket, data} = Mint.WebSocket.encode(websocket, :close)
{:ok, conn} = Mint.HTTP.stream_request_body(conn, ref, data)

message = receive(do: (message -> message))
{:ok, conn, [{:data, ^ref, data}]} = Mint.HTTP.stream(conn, message)
{:ok, websocket, [{:close, 1_000, ""}]} =
Mint.WebSocket.decode(websocket, data)

Mint.HTTP.close(conn)

0 comments on commit 9e758d6

Please sign in to comment.