@@ -50,22 +50,41 @@ defmodule Wind do
50
50
## Examples
51
51
52
52
iex> Wind.setup(conn, ref, http_reply_message)
53
- {:ok, conn, ref, websocket}
53
+ {:ok, conn, ref, websocket, response }
54
54
55
55
"""
56
56
@ spec setup ( Mint.HTTP . t ( ) , Mint.Types . request_ref ( ) , term , list ) ::
57
- { :ok , Mint.HTTP . t ( ) , Mint.Types . request_ref ( ) , Mint.WebSocket . t ( ) }
57
+ { :ok , Mint.HTTP . t ( ) , Mint.Types . request_ref ( ) , Mint.WebSocket . t ( ) , [ Mint.Types . response ] }
58
58
| { :error , Mint.HTTP . t ( ) , Mint.Types . error ( ) , [ Mint.Types . response ( ) ] }
59
59
| :unknown
60
60
def setup ( conn , ref , http_reply_message , opts \\ [ ] )
61
61
when not is_nil ( conn ) and not is_nil ( ref ) do
62
- with { :ok , conn , [ { :status , ^ ref , status } , { :headers , ^ ref , resp_headers } , { :done , ^ ref } ] } <-
63
- Mint.WebSocket . stream ( conn , http_reply_message ) ,
62
+ with { :ok , conn , response } <- Mint.WebSocket . stream ( conn , http_reply_message ) ,
63
+ % { status: status , headers: resp_headers } <- decode_setup_response ( response , ref ) ,
64
64
{ :ok , conn , websocket } <- Mint.WebSocket . new ( conn , ref , status , resp_headers , opts ) do
65
- { :ok , conn , ref , websocket }
65
+ { :ok , conn , ref , websocket , response }
66
66
end
67
67
end
68
68
69
+ defp decode_setup_response ( response , ref , out \\ % { } )
70
+
71
+ defp decode_setup_response ( [ { :status , response_ref , status } | tail ] , ref , out ) when response_ref == ref do
72
+ decode_setup_response ( tail , ref , Map . put ( out , :status , status ) )
73
+ end
74
+
75
+ defp decode_setup_response ( [ { :headers , response_ref , headers } | tail ] , ref , out ) when response_ref == ref do
76
+ decode_setup_response ( tail , ref , Map . update ( out , :headers , headers , fn existing -> [ headers | existing ] end ) )
77
+ end
78
+
79
+ defp decode_setup_response ( [ { :data , response_ref , data } | tail ] , ref , out ) when response_ref == ref do
80
+ decode_setup_response ( tail , ref , Map . update ( out , :data , data , fn existing -> [ data | existing ] end ) )
81
+ end
82
+
83
+ defp decode_setup_response ( [ { :done , response_ref } | tail ] , ref , out ) when response_ref == ref ,
84
+ do: decode_setup_response ( tail , ref , out )
85
+
86
+ defp decode_setup_response ( _ , _ , out ) , do: out
87
+
69
88
@ doc """
70
89
Synchronously setup a websocket connection. See `setup/3`.
71
90
@@ -76,7 +95,7 @@ defmodule Wind do
76
95
77
96
"""
78
97
@ spec setup_await ( Mint.HTTP . t ( ) , Mint.Types . request_ref ( ) ) ::
79
- { :ok , Mint.HTTP . t ( ) , Mint.Types . request_ref ( ) , Mint.WebSocket . t ( ) }
98
+ { :ok , Mint.HTTP . t ( ) , Mint.Types . request_ref ( ) , Mint.WebSocket . t ( ) , [ Mint.Types . response ( ) ] }
80
99
| { :error , Mint.HTTP . t ( ) , Mint.Types . error ( ) , [ Mint.Types . response ( ) ] }
81
100
| :unknown
82
101
def setup_await ( conn , ref )
0 commit comments