package main import ( "encoding/json" "flag" "fmt" "net/http" "time" "github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2/examples/internal/signal" ) func main() { addr := flag.String("address", ":50000", "Address to host the HTTP server on.") flag.Parse() // Everything below is the pion-WebRTC API! Thanks for using it ❤️. // Prepare the configuration config := webrtc.Configuration{ ICEServers: []webrtc.ICEServer{ { URLs: []string{"stun:stun.l.google.com:19302"}, }, }, } // Create a new RTCPeerConnection peerConnection, err := webrtc.NewPeerConnection(config) if err != nil { panic(err) } // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) // Register data channel creation handling peerConnection.OnDataChannel(func(d *webrtc.DataChannel) { fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID()) // Register channel opening handling d.OnOpen(func() { fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label(), d.ID()) for range time.NewTicker(5 * time.Second).C { message := signal.RandSeq(15) fmt.Printf("Sending '%s'\n", message) // Send the message as text sendTextErr := d.SendText(message) if sendTextErr != nil { panic(sendTextErr) } } }) // Register text message handling d.OnMessage(func(msg webrtc.DataChannelMessage) { fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label(), string(msg.Data)) }) }) // Exchange the offer/answer via HTTP offerChan, answerChan := mustSignalViaHTTP(*addr) // Wait for the remote SessionDescription offer := <-offerChan err = peerConnection.SetRemoteDescription(offer) if err != nil { panic(err) } // Create answer answer, err := peerConnection.CreateAnswer(nil) if err != nil { panic(err) } // Sets the LocalDescription, and starts our UDP listeners err = peerConnection.SetLocalDescription(answer) if err != nil { panic(err) } // Send the answer answerChan <- answer // Block forever select {} } // mustSignalViaHTTP exchange the SDP offer and answer using an HTTP server. func mustSignalViaHTTP(address string) (offerOut chan webrtc.SessionDescription, answerIn chan webrtc.SessionDescription) { offerOut = make(chan webrtc.SessionDescription) answerIn = make(chan webrtc.SessionDescription) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { var offer webrtc.SessionDescription err := json.NewDecoder(r.Body).Decode(&offer) if err != nil { panic(err) } offerOut <- offer answer := <-answerIn err = json.NewEncoder(w).Encode(answer) if err != nil { panic(err) } }) go func() { panic(http.ListenAndServe(address, nil)) }() fmt.Println("Listening on", address) return }