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:
8
examples/data-channels/jsfiddle/demo.css
Normal file
8
examples/data-channels/jsfiddle/demo.css
Normal file
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
textarea {
|
||||
width: 500px;
|
||||
min-height: 75px;
|
||||
}
|
||||
8
examples/data-channels/jsfiddle/demo.details
Normal file
8
examples/data-channels/jsfiddle/demo.details
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
# SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
name: data-channels
|
||||
description: Example of using Pion WebRTC to communicate with a web browser using bi-direction DataChannels
|
||||
authors:
|
||||
- Sean DuBois
|
||||
25
examples/data-channels/jsfiddle/demo.html
Normal file
25
examples/data-channels/jsfiddle/demo.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!--
|
||||
SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
SPDX-License-Identifier: MIT
|
||||
-->
|
||||
Browser base64 Session Description<br />
|
||||
<textarea id="localSessionDescription" readonly="true"></textarea> <br />
|
||||
<button onclick="window.copySDP()">
|
||||
Copy browser SDP to clipboard
|
||||
</button>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
Golang base64 Session Description<br />
|
||||
<textarea id="remoteSessionDescription"></textarea><br/>
|
||||
<button onclick="window.startSession()">Start Session</button><br />
|
||||
|
||||
<br />
|
||||
|
||||
Message<br />
|
||||
<textarea id="message">This is my DataChannel message!</textarea> <br/>
|
||||
<button onclick="window.sendMessage()">Send Message</button> <br />
|
||||
|
||||
<br />
|
||||
Logs<br />
|
||||
<div id="logs"></div>
|
||||
67
examples/data-channels/jsfiddle/demo.js
Normal file
67
examples/data-channels/jsfiddle/demo.js
Normal file
@@ -0,0 +1,67 @@
|
||||
/* eslint-env browser */
|
||||
|
||||
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
const pc = new RTCPeerConnection({
|
||||
iceServers: [
|
||||
{
|
||||
urls: 'stun:stun.l.google.com:19302'
|
||||
}
|
||||
]
|
||||
})
|
||||
const log = msg => {
|
||||
document.getElementById('logs').innerHTML += msg + '<br>'
|
||||
}
|
||||
|
||||
const sendChannel = pc.createDataChannel('foo')
|
||||
sendChannel.onclose = () => console.log('sendChannel has closed')
|
||||
sendChannel.onopen = () => console.log('sendChannel has opened')
|
||||
sendChannel.onmessage = e => log(`Message from DataChannel '${sendChannel.label}' payload '${e.data}'`)
|
||||
|
||||
pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
|
||||
pc.onicecandidate = event => {
|
||||
if (event.candidate === null) {
|
||||
document.getElementById('localSessionDescription').value = btoa(JSON.stringify(pc.localDescription))
|
||||
}
|
||||
}
|
||||
|
||||
pc.onnegotiationneeded = e =>
|
||||
pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
|
||||
|
||||
window.sendMessage = () => {
|
||||
const message = document.getElementById('message').value
|
||||
if (message === '') {
|
||||
return alert('Message must not be empty')
|
||||
}
|
||||
|
||||
sendChannel.send(message)
|
||||
}
|
||||
|
||||
window.startSession = () => {
|
||||
const sd = document.getElementById('remoteSessionDescription').value
|
||||
if (sd === '') {
|
||||
return alert('Session Description must not be empty')
|
||||
}
|
||||
|
||||
try {
|
||||
pc.setRemoteDescription(JSON.parse(atob(sd)))
|
||||
} catch (e) {
|
||||
alert(e)
|
||||
}
|
||||
}
|
||||
|
||||
window.copySDP = () => {
|
||||
const browserSDP = document.getElementById('localSessionDescription')
|
||||
|
||||
browserSDP.focus()
|
||||
browserSDP.select()
|
||||
|
||||
try {
|
||||
const successful = document.execCommand('copy')
|
||||
const msg = successful ? 'successful' : 'unsuccessful'
|
||||
log('Copying SDP was ' + msg)
|
||||
} catch (err) {
|
||||
log('Unable to copy SDP ' + err)
|
||||
}
|
||||
}
|
||||
202
examples/data-channels/jsfiddle/main.go
Normal file
202
examples/data-channels/jsfiddle/main.go
Normal file
@@ -0,0 +1,202 @@
|
||||
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//go:build js && wasm
|
||||
// +build js,wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Configure and create a new PeerConnection.
|
||||
config := webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
},
|
||||
},
|
||||
}
|
||||
pc, err := webrtc.NewPeerConnection(config)
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
|
||||
// Create DataChannel.
|
||||
sendChannel, err := pc.CreateDataChannel("foo", nil)
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
sendChannel.OnClose(func() {
|
||||
fmt.Println("sendChannel has closed")
|
||||
})
|
||||
sendChannel.OnClosing(func() {
|
||||
fmt.Println("sendChannel is closing")
|
||||
})
|
||||
sendChannel.OnError(func(err error) {
|
||||
fmt.Println("sendChannel error", err)
|
||||
})
|
||||
sendChannel.OnOpen(func() {
|
||||
fmt.Println("sendChannel has opened")
|
||||
|
||||
candidatePair, err := pc.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
|
||||
|
||||
fmt.Println(candidatePair)
|
||||
fmt.Println(err)
|
||||
})
|
||||
sendChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
|
||||
log(fmt.Sprintf("Message from DataChannel %s payload %s", sendChannel.Label(), string(msg.Data)))
|
||||
})
|
||||
|
||||
// Create offer
|
||||
offer, err := pc.CreateOffer(nil)
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
if err := pc.SetLocalDescription(offer); err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
|
||||
// Add handlers for setting up the connection.
|
||||
pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
||||
log(fmt.Sprint(state))
|
||||
})
|
||||
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
if candidate != nil {
|
||||
encodedDescr := encode(pc.LocalDescription())
|
||||
el := getElementByID("localSessionDescription")
|
||||
el.Set("value", encodedDescr)
|
||||
}
|
||||
})
|
||||
|
||||
// Set up global callbacks which will be triggered on button clicks.
|
||||
js.Global().Set("sendMessage", js.FuncOf(func(_ js.Value, _ []js.Value) any {
|
||||
go func() {
|
||||
el := getElementByID("message")
|
||||
message := el.Get("value").String()
|
||||
if message == "" {
|
||||
js.Global().Call("alert", "Message must not be empty")
|
||||
return
|
||||
}
|
||||
if err := sendChannel.SendText(message); err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
}()
|
||||
return js.Undefined()
|
||||
}))
|
||||
js.Global().Set("startSession", js.FuncOf(func(_ js.Value, _ []js.Value) any {
|
||||
go func() {
|
||||
el := getElementByID("remoteSessionDescription")
|
||||
sd := el.Get("value").String()
|
||||
if sd == "" {
|
||||
js.Global().Call("alert", "Session Description must not be empty")
|
||||
return
|
||||
}
|
||||
|
||||
descr := webrtc.SessionDescription{}
|
||||
decode(sd, &descr)
|
||||
if err := pc.SetRemoteDescription(descr); err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
}()
|
||||
return js.Undefined()
|
||||
}))
|
||||
js.Global().Set("copySDP", js.FuncOf(func(_ js.Value, _ []js.Value) any {
|
||||
go func() {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
switch e := e.(type) {
|
||||
case error:
|
||||
handleError(e)
|
||||
default:
|
||||
handleError(fmt.Errorf("recovered with non-error value: (%T) %s", e, e))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
browserSDP := getElementByID("localSessionDescription")
|
||||
|
||||
browserSDP.Call("focus")
|
||||
browserSDP.Call("select")
|
||||
|
||||
copyStatus := js.Global().Get("document").Call("execCommand", "copy")
|
||||
if copyStatus.Bool() {
|
||||
log("Copying SDP was successful")
|
||||
} else {
|
||||
log("Copying SDP was unsuccessful")
|
||||
}
|
||||
}()
|
||||
return js.Undefined()
|
||||
}))
|
||||
|
||||
// Stay alive
|
||||
select {}
|
||||
}
|
||||
|
||||
func log(msg string) {
|
||||
el := getElementByID("logs")
|
||||
el.Set("innerHTML", el.Get("innerHTML").String()+msg+"<br>")
|
||||
}
|
||||
|
||||
func handleError(err error) {
|
||||
log("Unexpected error. Check console.")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
func getElementByID(id string) js.Value {
|
||||
return js.Global().Get("document").Call("getElementById", id)
|
||||
}
|
||||
|
||||
// Read from stdin until we get a newline
|
||||
func readUntilNewline() (in string) {
|
||||
var err error
|
||||
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
in, err = r.ReadString('\n')
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if in = strings.TrimSpace(in); len(in) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
return
|
||||
}
|
||||
|
||||
// JSON encode + base64 a SessionDescription
|
||||
func encode(obj *webrtc.SessionDescription) string {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// Decode a base64 and unmarshal JSON into a SessionDescription
|
||||
func decode(in string, obj *webrtc.SessionDescription) {
|
||||
b, err := base64.StdEncoding.DecodeString(in)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(b, obj); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user