1
- Mix . install ( [ { :gun , "~> 2.0.1" } , { :ex_ice , path: "../" } , { :jason , "~> 1.4.0" } ] )
1
+ Mix . install ( [ { :gun , "~> 2.0.1" } , { :ex_ice , path: "../" , force: true } , { :jason , "~> 1.4.0" } ] )
2
+
3
+ require Logger
4
+ Logger . configure ( level: :info )
2
5
3
6
defmodule Peer do
4
7
use GenServer
@@ -31,7 +34,8 @@ defmodule Peer do
31
34
receive do
32
35
{ :gun_upgrade , ^ conn , stream , _ , _ } ->
33
36
Logger . info ( "Connected to the signalling server" )
34
- { :ok , % { conn: conn , stream: stream , ice: nil } }
37
+ Process . send_after ( self ( ) , :ws_ping , 1000 )
38
+ { :ok , % { conn: conn , stream: stream , ice: nil , timer: nil } }
35
39
36
40
other ->
37
41
Logger . error ( "Couldn't connect to the signalling server: #{ inspect ( other ) } " )
@@ -48,21 +52,41 @@ defmodule Peer do
48
52
{ :stop , :normal , state }
49
53
end
50
54
55
+ @ impl true
56
+ def handle_info ( :ws_ping , state ) do
57
+ Process . send_after ( self ( ) , :ws_ping , 1000 )
58
+ :gun . ws_send ( state . conn , state . stream , :ping )
59
+ { :noreply , state }
60
+ end
61
+
51
62
@ impl true
52
63
def handle_info ( { :gun_ws , _ , _ , { :text , msg } } , state ) do
53
64
state = handle_ws_msg ( Jason . decode! ( msg ) , state )
54
65
{ :noreply , state }
55
66
end
56
67
68
+ @ impl true
69
+ def handle_info ( { :gun_ws , _ , _ , { :close , code } } , _state ) do
70
+ Logger . info ( "Signalling connection closed with code: #{ code } . Exiting" )
71
+ exit ( :ws_down )
72
+ end
73
+
57
74
@ impl true
58
75
def handle_info ( { :ex_ice , _pid , msg } , state ) do
59
76
state = handle_ice_msg ( msg , state )
60
77
{ :noreply , state }
61
78
end
62
79
80
+ @ impl true
81
+ def handle_info ( :send_ping , state ) do
82
+ ref = Process . send_after ( self ( ) , :send_ping , 1000 )
83
+ :ok = ICEAgent . send_data ( state . ice , "ping" )
84
+ { :noreply , % { state | timer: ref } }
85
+ end
86
+
63
87
@ impl true
64
88
def handle_info ( msg , state ) do
65
- Logger . warn ( "Received unknown msg: #{ inspect ( msg ) } " )
89
+ Logger . warning ( "Received unknown msg: #{ inspect ( msg ) } " )
66
90
{ :noreply , state }
67
91
end
68
92
@@ -79,14 +103,17 @@ defmodule Peer do
79
103
stun_servers: [ "stun:stun.l.google.com:19302" ]
80
104
)
81
105
82
- :ok = ICEAgent . run ( pid )
106
+ { :ok , ufrag , passwd } = ICEAgent . get_local_credentials ( pid )
107
+
108
+ msg = % { type: "credentials" , ufrag: ufrag , passwd: passwd } |> Jason . encode! ( )
109
+ :gun . ws_send ( state . conn , state . stream , { :text , msg } )
110
+
111
+ :ok = ICEAgent . gather_candidates ( pid )
83
112
% { state | ice: pid }
84
113
end
85
114
86
115
defp handle_ws_msg ( % { "type" => "credentials" , "ufrag" => ufrag , "passwd" => passwd } , state ) do
87
- :ok =
88
- ICEAgent . set_remote_credentials ( state . ice , Base . decode64! ( ufrag ) , Base . decode64! ( passwd ) )
89
-
116
+ :ok = ICEAgent . set_remote_credentials ( state . ice , ufrag , passwd )
90
117
state
91
118
end
92
119
@@ -100,34 +127,33 @@ defmodule Peer do
100
127
state
101
128
end
102
129
103
- def handle_ice_msg ( { :local_credentials , ufrag , passwd } , state ) do
104
- msg =
105
- % { type: "credentials" , ufrag: Base . encode64 ( ufrag ) , passwd: Base . encode64 ( passwd ) }
106
- |> Jason . encode! ( )
107
-
108
- :gun . ws_send ( state . conn , state . stream , { :text , msg } )
109
- state
110
- end
111
-
112
130
def handle_ice_msg ( { :new_candidate , cand } , state ) do
131
+
113
132
msg = % { type: "candidate" , cand: cand } |> Jason . encode! ( )
114
133
:gun . ws_send ( state . conn , state . stream , { :text , msg } )
115
134
state
116
135
end
117
136
118
- def handle_ice_msg ( :gathering_complete , state ) do
137
+ def handle_ice_msg ( { :gathering_state_change , :complete } = msg , state ) do
138
+ Logger . info ( "ICE: #{ inspect ( msg ) } " )
119
139
msg = % { type: "end_of_candidates" } |> Jason . encode! ( )
120
140
:gun . ws_send ( state . conn , state . stream , { :text , msg } )
121
141
state
122
142
end
123
143
144
+ def handle_ice_msg ( :completed , state ) do
145
+ Logger . info ( "ICE: :completed" )
146
+ Logger . info ( "Starting sending..." )
147
+ ref = Process . send_after ( self ( ) , :send_ping , 1000 )
148
+ % { state | timer: ref }
149
+ end
150
+
124
151
def handle_ice_msg ( other , state ) do
125
152
Logger . info ( "ICE: #{ inspect ( other ) } " )
126
153
state
127
154
end
128
155
end
129
156
130
- require Logger
131
157
132
158
{ :ok , pid } = Peer . start_link ( )
133
159
ref = Process . monitor ( pid )
@@ -137,5 +163,5 @@ receive do
137
163
Logger . info ( "Peer process closed. Exiting" )
138
164
139
165
other ->
140
- Logger . warn ( "Unexpected msg. Exiting. Msg: #{ inspect ( other ) } " )
166
+ Logger . warning ( "Unexpected msg. Exiting. Msg: #{ inspect ( other ) } " )
141
167
end
0 commit comments