Skip to content

Instantly share code, notes, and snippets.

@Sean-Der
Created November 11, 2019 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sean-Der/4b7db289963fffac407996d568b5c6c1 to your computer and use it in GitHub Desktop.
Save Sean-Der/4b7db289963fffac407996d568b5c6c1 to your computer and use it in GitHub Desktop.
Working Server + WASM Pion
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