|
| 1 | +# see https://websocket.org/echo.html |
| 2 | +{:ok, conn} = Mint.HTTP.connect(:https, "echo.websocket.org", 443) |
| 3 | + |
| 4 | +{:ok, conn, ref} = Mint.WebSocket.upgrade(conn, "/", []) |
| 5 | + |
| 6 | +message = receive(do: (message -> message)) |
| 7 | +{:ok, conn, [{:status, ^ref, status}, {:headers, ^ref, resp_headers}, {:done, ^ref}]} = |
| 8 | + Mint.HTTP.stream(conn, message) |
| 9 | +{:ok, conn, websocket} = Mint.WebSocket.new(conn, ref, status, resp_headers) |
| 10 | + |
| 11 | +frame = {:text, "Rock it with Mint.WebSocket"} |
| 12 | + |
| 13 | +{:ok, websocket, data} = Mint.WebSocket.encode(websocket, frame) |
| 14 | +{:ok, conn} = Mint.HTTP.stream_request_body(conn, ref, data) |
| 15 | + |
| 16 | +message = receive(do: (message -> message)) |
| 17 | +{:ok, conn, [{:data, ^ref, data}]} = Mint.HTTP.stream(conn, message) |
| 18 | +{:ok, websocket, [{:text, "Rock it with Mint.WebSocket"}]} = |
| 19 | + Mint.WebSocket.decode(websocket, data) |
| 20 | + |
| 21 | +{:ok, websocket, data} = Mint.WebSocket.encode(websocket, :close) |
| 22 | +{:ok, conn} = Mint.HTTP.stream_request_body(conn, ref, data) |
| 23 | + |
| 24 | +message = receive(do: (message -> message)) |
| 25 | +{:ok, conn, [{:data, ^ref, data}]} = Mint.HTTP.stream(conn, message) |
| 26 | +{:ok, websocket, [{:close, 1_000, ""}]} = |
| 27 | + Mint.WebSocket.decode(websocket, data) |
| 28 | + |
| 29 | +Mint.HTTP.close(conn) |
0 commit comments