snapshot of https://github.com/pion/webrtc.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
REUSE Compliance Check / lint (push) Failing after 0s
Browser E2E / Test (push) Failing after 3s
Test / test (1.24) (push) Failing after 0s
Test / test-i386 (1.25) (push) Failing after 0s
Test / test-i386 (1.24) (push) Failing after 0s
Test / test-windows (1.25) (push) Failing after 0s
Test / test-windows (1.24) (push) Failing after 0s
Test / test (1.25) (push) Failing after 0s
Examples Tests / Test (push) Failing after 3s
Test / test-macos (1.25) (push) Failing after 0s
Test / test-macos (1.24) (push) Failing after 0s
Go mod tidy / tidy (push) Failing after 0s
Test / test-wasm (push) Failing after 0s
CodeQL / analyze (push) Failing after 1s
Fuzz / fuzz (push) Failing after 0s
Some checks failed
REUSE Compliance Check / lint (push) Failing after 0s
Browser E2E / Test (push) Failing after 3s
Test / test (1.24) (push) Failing after 0s
Test / test-i386 (1.25) (push) Failing after 0s
Test / test-i386 (1.24) (push) Failing after 0s
Test / test-windows (1.25) (push) Failing after 0s
Test / test-windows (1.24) (push) Failing after 0s
Test / test (1.25) (push) Failing after 0s
Examples Tests / Test (push) Failing after 3s
Test / test-macos (1.25) (push) Failing after 0s
Test / test-macos (1.24) (push) Failing after 0s
Go mod tidy / tidy (push) Failing after 0s
Test / test-wasm (push) Failing after 0s
CodeQL / analyze (push) Failing after 1s
Fuzz / fuzz (push) Failing after 0s
This commit is contained in:
26
examples/ice-single-port/README.md
Normal file
26
examples/ice-single-port/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# ice-single-port
|
||||
ice-single-port demonstrates Pion WebRTC's ability to serve many PeerConnections on a single port.
|
||||
|
||||
Pion WebRTC has no global state, so by default ports can't be shared between two PeerConnections.
|
||||
Using the SettingEngine, a developer can manually share state between many PeerConnections to allow
|
||||
multiple PeerConnections to use the same port.
|
||||
|
||||
## Instructions
|
||||
|
||||
### Download ice-single-port
|
||||
This example requires you to clone the repo since it is serving static HTML.
|
||||
|
||||
```
|
||||
git clone https://github.com/pion/webrtc.git
|
||||
cd webrtc/examples/ice-single-port
|
||||
```
|
||||
|
||||
### Run ice-single-port
|
||||
Execute `go run *.go`
|
||||
|
||||
### Open the Web UI
|
||||
Open [http://localhost:8080](http://localhost:8080). This will automatically open 10 PeerConnections. This page will print
|
||||
a Local/Remote line for each PeerConnection. Note that all 10 PeerConnections have different ports for their Local port.
|
||||
However for the remote they all will be using port 8443.
|
||||
|
||||
Congrats, you have used Pion WebRTC! Now start building something cool.
|
||||
56
examples/ice-single-port/index.html
Normal file
56
examples/ice-single-port/index.html
Normal file
@@ -0,0 +1,56 @@
|
||||
<html>
|
||||
<!--
|
||||
SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
SPDX-License-Identifier: MIT
|
||||
-->
|
||||
<head>
|
||||
<title>ice-single-port</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h3> ICE Selected Pairs </h3>
|
||||
<div id="iceSelectedPairs"></div> <br />
|
||||
</body>
|
||||
|
||||
<script>
|
||||
let createPeerConnection = () => {
|
||||
let pc = new RTCPeerConnection()
|
||||
let dc = pc.createDataChannel('data')
|
||||
|
||||
dc.onopen = () => {
|
||||
let el = document.createElement('template')
|
||||
let selectedPair = pc.sctp.transport.iceTransport.getSelectedCandidatePair()
|
||||
|
||||
el.innerHTML = `<div>
|
||||
<ul>
|
||||
<li> <i> Local</i> - ${selectedPair.local.candidate}</li>
|
||||
<li> <i> Remote</i> - ${selectedPair.remote.candidate} </li>
|
||||
</ul>
|
||||
</div>`
|
||||
|
||||
document.getElementById('iceSelectedPairs').appendChild(el.content.firstChild);
|
||||
}
|
||||
|
||||
pc.createOffer()
|
||||
.then(offer => {
|
||||
pc.setLocalDescription(offer)
|
||||
|
||||
return fetch(`/doSignaling`, {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(offer)
|
||||
})
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(res => pc.setRemoteDescription(res))
|
||||
.catch(alert)
|
||||
}
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
createPeerConnection()
|
||||
}
|
||||
</script>
|
||||
</html>
|
||||
104
examples/ice-single-port/main.go
Normal file
104
examples/ice-single-port/main.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//go:build !js
|
||||
|
||||
// ice-single-port demonstrates Pion WebRTC's ability to serve many PeerConnections on a single port.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/pion/ice/v4"
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
var api *webrtc.API //nolint
|
||||
|
||||
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
|
||||
func doSignaling(res http.ResponseWriter, req *http.Request) {
|
||||
peerConnection, err := api.NewPeerConnection(webrtc.Configuration{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
||||
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
|
||||
})
|
||||
|
||||
// Send the current time via a DataChannel to the remote peer every 3 seconds
|
||||
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
|
||||
d.OnOpen(func() {
|
||||
for range time.Tick(time.Second * 3) {
|
||||
if err = d.SendText(time.Now().String()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
var offer webrtc.SessionDescription
|
||||
if err = json.NewDecoder(req.Body).Decode(&offer); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = peerConnection.SetRemoteDescription(offer); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create channel that is blocked until ICE Gathering is complete
|
||||
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
|
||||
|
||||
answer, err := peerConnection.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
} else if err = peerConnection.SetLocalDescription(answer); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Block until ICE Gathering is complete, disabling trickle ICE
|
||||
// we do this because we only can exchange one signaling message
|
||||
// in a production application you should exchange ICE Candidates via OnICECandidate
|
||||
<-gatherComplete
|
||||
|
||||
response, err := json.Marshal(*peerConnection.LocalDescription())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
res.Header().Set("Content-Type", "application/json")
|
||||
if _, err := res.Write(response); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Create a SettingEngine, this allows non-standard WebRTC behavior
|
||||
settingEngine := webrtc.SettingEngine{}
|
||||
|
||||
// Configure our SettingEngine to use our UDPMux. By default a PeerConnection has
|
||||
// no global state. The API+SettingEngine allows the user to share state between them.
|
||||
// In this case we are sharing our listening port across many.
|
||||
// Listen on UDP Port 8443, will be used for all WebRTC traffic
|
||||
mux, err := ice.NewMultiUDPMuxFromPort(8443)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("Listening for WebRTC traffic at %d\n", 8443)
|
||||
settingEngine.SetICEUDPMux(mux)
|
||||
|
||||
// Create a new API using our SettingEngine
|
||||
api = webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
|
||||
|
||||
http.Handle("/", http.FileServer(http.Dir(".")))
|
||||
http.HandleFunc("/doSignaling", doSignaling)
|
||||
|
||||
fmt.Println("Open http://localhost:8080 to access this demo")
|
||||
// nolint: gosec
|
||||
panic(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
Reference in New Issue
Block a user