Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"github.com/pion/webrtc/v2"
)
func main() {
// Create two PeerConnections
offerPc, _ := webrtc.NewPeerConnection(webrtc.Configuration{})
answerPc, _ := webrtc.NewPeerConnection(webrtc.Configuration{})
// Create a DataChannel on your offering PeerConnection
answerDataChannel, _ := answerPc.CreateDataChannel("demoChannel", nil)
// Set OnICECandidate handlers to exchange between peers
offerPc.OnICECandidate(func(c *webrtc.ICECandidate) {
if c != nil {
answerPc.AddICECandidate(c.ToJSON())
}
})
answerPc.OnICECandidate(func(c *webrtc.ICECandidate) {
if c != nil {
offerPc.AddICECandidate(c.ToJSON())
}
})
// Generate Offer, SetLocalDescription and SetRemoteDescription
offer, _ := offerPc.CreateOffer(nil)
offerPc.SetLocalDescription(offer)
answerPc.SetRemoteDescription(offer)
// Generate Answer, SetLocalDescription and SetRemoteDescription
answer, _ := answerPc.CreateAnswer(nil)
answerPc.SetLocalDescription(answer)
offerPc.SetRemoteDescription(answer)
// OnMessage handler for the DataChannel you just created
answerDataChannel.OnMessage(func(message webrtc.DataChannelMessage) {
fmt.Printf("OnMessage: %s \n", string(message.Data))
})
// On the other Peer wait for that DataChannel to be created
offerPc.OnDataChannel(func(offerDataChannel *webrtc.DataChannel) {
fmt.Printf("OnDataChannel: %s \n", offerDataChannel.Label())
// When the DataChannel is open send a message
offerDataChannel.OnOpen(func() {
offerDataChannel.SendText("Thank you for flying Pion!")
})
})
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.