mirror of
https://github.com/pion/webrtc.git
synced 2025-10-27 09:11:19 +08:00
104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/pions/datachannel"
|
|
"github.com/pions/webrtc"
|
|
"github.com/pions/webrtc/examples/util"
|
|
"github.com/pions/webrtc/pkg/ice"
|
|
)
|
|
|
|
const messageSize = 15
|
|
|
|
func main() {
|
|
// Since this behavior diverges from the WebRTC API it has to be
|
|
// enabled using global switch.
|
|
// Mixing both behaviors is not supported.
|
|
webrtc.DetachDataChannels()
|
|
|
|
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
|
|
|
|
// Prepare the configuration
|
|
config := webrtc.RTCConfiguration{
|
|
IceServers: []webrtc.RTCIceServer{
|
|
{
|
|
URLs: []string{"stun:stun.l.google.com:19302"},
|
|
},
|
|
},
|
|
}
|
|
|
|
// Create a new RTCPeerConnection
|
|
peerConnection, err := webrtc.New(config)
|
|
util.Check(err)
|
|
|
|
// Create a datachannel with label 'data'
|
|
dataChannel, err := peerConnection.CreateDataChannel("data", nil)
|
|
util.Check(err)
|
|
|
|
// Set the handler for ICE connection state
|
|
// This will notify you when the peer has connected/disconnected
|
|
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
|
|
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
|
|
})
|
|
|
|
// Register channel opening handling
|
|
dataChannel.OnOpen(func() {
|
|
fmt.Printf("Data channel '%s'-'%d' open.\n", dataChannel.Label, dataChannel.ID)
|
|
|
|
// Detach the data channel
|
|
raw, dErr := dataChannel.Detach()
|
|
util.Check(dErr)
|
|
|
|
// Handle reading from the data channel
|
|
go ReadLoop(raw)
|
|
|
|
// Handle writing to the data channel
|
|
go WriteLoop(raw)
|
|
})
|
|
|
|
// Create an offer to send to the browser
|
|
offer, err := peerConnection.CreateOffer(nil)
|
|
util.Check(err)
|
|
|
|
// Output the offer in base64 so we can paste it in browser
|
|
fmt.Println(util.Encode(offer))
|
|
|
|
// Wait for the answer to be pasted
|
|
answer := webrtc.RTCSessionDescription{}
|
|
util.Decode(util.MustReadStdin(), answer)
|
|
|
|
// Apply the answer as the remote description
|
|
err = peerConnection.SetRemoteDescription(answer)
|
|
util.Check(err)
|
|
|
|
// Block forever
|
|
select {}
|
|
}
|
|
|
|
// ReadLoop shows how to read from the datachannel directly
|
|
func ReadLoop(d *datachannel.DataChannel) {
|
|
for {
|
|
buffer := make([]byte, messageSize)
|
|
n, err := d.Read(buffer)
|
|
if err != nil {
|
|
fmt.Println("Datachannel closed; Exit the readloop:", err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Message from DataChannel '%s': %s\n", d.Label, string(buffer[:n]))
|
|
}
|
|
}
|
|
|
|
// WriteLoop shows how to write to the datachannel directly
|
|
func WriteLoop(d *datachannel.DataChannel) {
|
|
for range time.NewTicker(5 * time.Second).C {
|
|
message := util.RandSeq(messageSize)
|
|
fmt.Printf("Sending %s \n", message)
|
|
|
|
_, err := d.Write([]byte(message))
|
|
util.Check(err)
|
|
}
|
|
}
|