@@ -4,6 +4,7 @@ package main
44
55import (
66 "fmt"
7+ "os"
78
89 "github.com/pion/logging"
910 "github.com/pion/webrtc/v3"
@@ -60,6 +61,11 @@ func main() {
6061 if err != nil {
6162 panic (err )
6263 }
64+ defer func () {
65+ if cErr := offerPeerConnection .Close (); cErr != nil {
66+ fmt .Printf ("cannot close offerPeerConnection: %v\n " , cErr )
67+ }
68+ }()
6369
6470 // We need a DataChannel so we can have ICE Candidates
6571 if _ , err = offerPeerConnection .CreateDataChannel ("custom-logger" , nil ); err != nil {
@@ -71,6 +77,39 @@ func main() {
7177 if err != nil {
7278 panic (err )
7379 }
80+ defer func () {
81+ if cErr := answerPeerConnection .Close (); cErr != nil {
82+ fmt .Printf ("cannot close answerPeerConnection: %v\n " , cErr )
83+ }
84+ }()
85+
86+ // Set the handler for Peer connection state
87+ // This will notify you when the peer has connected/disconnected
88+ offerPeerConnection .OnConnectionStateChange (func (s webrtc.PeerConnectionState ) {
89+ fmt .Printf ("Peer Connection State has changed: %s (offerer)\n " , s .String ())
90+
91+ if s == webrtc .PeerConnectionStateFailed {
92+ // Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
93+ // Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
94+ // Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
95+ fmt .Println ("Peer Connection has gone to failed exiting" )
96+ os .Exit (0 )
97+ }
98+ })
99+
100+ // Set the handler for Peer connection state
101+ // This will notify you when the peer has connected/disconnected
102+ answerPeerConnection .OnConnectionStateChange (func (s webrtc.PeerConnectionState ) {
103+ fmt .Printf ("Peer Connection State has changed: %s (answerer)\n " , s .String ())
104+
105+ if s == webrtc .PeerConnectionStateFailed {
106+ // Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
107+ // Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
108+ // Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
109+ fmt .Println ("Peer Connection has gone to failed exiting" )
110+ os .Exit (0 )
111+ }
112+ })
74113
75114 // Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
76115 // send it to the other peer
@@ -126,5 +165,6 @@ func main() {
126165 panic (err )
127166 }
128167
168+ // Block forever
129169 select {}
130170}
0 commit comments