mirror of
https://github.com/pion/webrtc.git
synced 2025-10-29 01:42:59 +08:00
committed by
Michiel De Backker
parent
bf422e0c0a
commit
ddcef2d84f
@@ -6,7 +6,8 @@ import (
|
||||
|
||||
"github.com/pions/datachannel"
|
||||
"github.com/pions/webrtc"
|
||||
"github.com/pions/webrtc/examples/util"
|
||||
|
||||
"github.com/pions/webrtc/examples/internal/signal"
|
||||
)
|
||||
|
||||
const messageSize = 15
|
||||
@@ -30,11 +31,15 @@ func main() {
|
||||
|
||||
// Create a new RTCPeerConnection
|
||||
peerConnection, err := webrtc.NewPeerConnection(config)
|
||||
util.Check(err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create a datachannel with label 'data'
|
||||
dataChannel, err := peerConnection.CreateDataChannel("data", nil)
|
||||
util.Check(err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
@@ -48,7 +53,9 @@ func main() {
|
||||
|
||||
// Detach the data channel
|
||||
raw, dErr := dataChannel.Detach()
|
||||
util.Check(dErr)
|
||||
if dErr != nil {
|
||||
panic(dErr)
|
||||
}
|
||||
|
||||
// Handle reading from the data channel
|
||||
go ReadLoop(raw)
|
||||
@@ -59,22 +66,28 @@ func main() {
|
||||
|
||||
// Create an offer to send to the browser
|
||||
offer, err := peerConnection.CreateOffer(nil)
|
||||
util.Check(err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
err = peerConnection.SetLocalDescription(offer)
|
||||
util.Check(err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Output the offer in base64 so we can paste it in browser
|
||||
fmt.Println(util.Encode(offer))
|
||||
fmt.Println(signal.Encode(offer))
|
||||
|
||||
// Wait for the answer to be pasted
|
||||
answer := webrtc.SessionDescription{}
|
||||
util.Decode(util.MustReadStdin(), answer)
|
||||
signal.Decode(signal.MustReadStdin(), answer)
|
||||
|
||||
// Apply the answer as the remote description
|
||||
err = peerConnection.SetRemoteDescription(answer)
|
||||
util.Check(err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Block forever
|
||||
select {}
|
||||
@@ -97,10 +110,12 @@ func ReadLoop(d *datachannel.DataChannel) {
|
||||
// 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)
|
||||
message := signal.RandSeq(messageSize)
|
||||
fmt.Printf("Sending %s \n", message)
|
||||
|
||||
_, err := d.Write([]byte(message))
|
||||
util.Check(err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user