Skip to content

Commit b458528

Browse files
committed
Simplify examples/ice-tcp
Example was copied from play-from-disk-renegotation and contained a global PeerConnection. Instead create a PeerConnection on each inbound request
1 parent 94262c1 commit b458528

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

examples/ice-tcp/main.go

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,43 @@ package main
55

66
import (
77
"encoding/json"
8+
"errors"
89
"fmt"
10+
"io"
911
"net"
1012
"net/http"
1113
"time"
1214

1315
"github.com/pion/webrtc/v3"
1416
)
1517

16-
var peerConnection *webrtc.PeerConnection //nolint
18+
var api *webrtc.API //nolint
1719

1820
func doSignaling(w http.ResponseWriter, r *http.Request) {
19-
var err error
20-
21-
if peerConnection == nil {
22-
settingEngine := webrtc.SettingEngine{}
23-
24-
// Enable support only for TCP ICE candidates.
25-
settingEngine.SetNetworkTypes([]webrtc.NetworkType{
26-
webrtc.NetworkTypeTCP4,
27-
webrtc.NetworkTypeTCP6,
28-
})
29-
30-
var tcpListener net.Listener
31-
tcpListener, err = net.ListenTCP("tcp", &net.TCPAddr{
32-
IP: net.IP{0, 0, 0, 0},
33-
Port: 8443,
34-
})
35-
36-
if err != nil {
37-
panic(err)
38-
}
39-
40-
fmt.Printf("Listening for ICE TCP at %s\n", tcpListener.Addr())
41-
42-
tcpMux := webrtc.NewICETCPMux(nil, tcpListener, 8)
43-
settingEngine.SetICETCPMux(tcpMux)
44-
45-
api := webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
46-
if peerConnection, err = api.NewPeerConnection(webrtc.Configuration{}); err != nil {
47-
panic(err)
48-
}
49-
50-
// Set the handler for ICE connection state
51-
// This will notify you when the peer has connected/disconnected
52-
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
53-
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
54-
})
21+
peerConnection, err := api.NewPeerConnection(webrtc.Configuration{})
22+
if err != nil {
23+
panic(err)
24+
}
5525

56-
// Send the current time via a DataChannel to the remote peer every 3 seconds
57-
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
58-
d.OnOpen(func() {
59-
for range time.Tick(time.Second * 3) {
60-
if err = d.SendText(time.Now().String()); err != nil {
61-
panic(err)
26+
// Set the handler for ICE connection state
27+
// This will notify you when the peer has connected/disconnected
28+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
29+
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
30+
})
31+
32+
// Send the current time via a DataChannel to the remote peer every 3 seconds
33+
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
34+
d.OnOpen(func() {
35+
for range time.Tick(time.Second * 3) {
36+
if err = d.SendText(time.Now().String()); err != nil {
37+
if errors.Is(io.ErrClosedPipe, err) {
38+
return
6239
}
40+
panic(err)
6341
}
64-
})
42+
}
6543
})
66-
}
44+
})
6745

6846
var offer webrtc.SessionDescription
6947
if err = json.NewDecoder(r.Body).Decode(&offer); err != nil {
@@ -101,6 +79,29 @@ func doSignaling(w http.ResponseWriter, r *http.Request) {
10179
}
10280

10381
func main() {
82+
settingEngine := webrtc.SettingEngine{}
83+
84+
// Enable support only for TCP ICE candidates.
85+
settingEngine.SetNetworkTypes([]webrtc.NetworkType{
86+
webrtc.NetworkTypeTCP4,
87+
webrtc.NetworkTypeTCP6,
88+
})
89+
90+
tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
91+
IP: net.IP{0, 0, 0, 0},
92+
Port: 8443,
93+
})
94+
if err != nil {
95+
panic(err)
96+
}
97+
98+
fmt.Printf("Listening for ICE TCP at %s\n", tcpListener.Addr())
99+
100+
tcpMux := webrtc.NewICETCPMux(nil, tcpListener, 8)
101+
settingEngine.SetICETCPMux(tcpMux)
102+
103+
api = webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
104+
104105
http.Handle("/", http.FileServer(http.Dir(".")))
105106
http.HandleFunc("/doSignaling", doSignaling)
106107

0 commit comments

Comments
 (0)