Created
February 1, 2020 07:27
-
-
Save Sean-Der/14972d662ef8b2d105c440505171dda5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"github.com/pion/webrtc/v2" | |
"github.com/pion/webrtc/v2/examples/internal/signal" | |
) | |
func main() { | |
// 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()) | |
}) | |
/////////////////////////////// | |
/////////////////////////////// | |
/////////////////////////////// | |
negotiated := true | |
// ttl | |
ttlID := uint16(0) | |
ttlOrdered := false | |
ttlMaxPacketLifeTime := uint16(300) | |
ttlConfig := &webrtc.DataChannelInit{ | |
ID: &ttlID, | |
Negotiated: &negotiated, | |
Ordered: &ttlOrdered, | |
MaxPacketLifeTime: &ttlMaxPacketLifeTime, | |
MaxRetransmits: nil, | |
} | |
ttl, err := peerConnection.CreateDataChannel("ttl", ttlConfig) | |
if err != nil { | |
panic(err) | |
} | |
ttlRecv := 0 | |
// Register text message handling | |
ttl.OnMessage(func(msg webrtc.DataChannelMessage) { | |
ttlRecv++ | |
}) | |
// reliable | |
reliableID := uint16(1) | |
reliableOrdered := true | |
reliableConfig := &webrtc.DataChannelInit{ | |
ID: &reliableID, | |
Negotiated: &negotiated, | |
Ordered: &reliableOrdered, | |
MaxPacketLifeTime: nil, | |
MaxRetransmits: nil, | |
} | |
reliable, err := peerConnection.CreateDataChannel("reliable", reliableConfig) | |
if err != nil { | |
panic(err) | |
} | |
reliableSent := 0 | |
reliableRecv := 0 | |
// Register channel opening handling | |
reliable.OnOpen(func() { | |
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", reliable.Label(), reliable.ID()) | |
for range time.NewTicker(2 * time.Second).C { | |
message := signal.RandSeq(40) | |
// Send the message as text | |
sendErr := reliable.SendText(message) | |
reliableSent++ | |
if sendErr != nil { | |
panic(sendErr) | |
} | |
} | |
}) | |
// Register text message handling | |
reliable.OnMessage(func(msg webrtc.DataChannelMessage) { | |
fmt.Printf("Message from DataChannel '%s': '%s'\n", reliable.Label(), string(msg.Data)) | |
}) | |
// tryOnce | |
tryOnceID := uint16(2) | |
tryOnceOrdered := false | |
tryOnceMaxRetransmits := uint16(0) | |
tryOnceConfig := &webrtc.DataChannelInit{ | |
ID: &tryOnceID, | |
Negotiated: &negotiated, | |
Ordered: &tryOnceOrdered, | |
MaxPacketLifeTime: nil, | |
MaxRetransmits: &tryOnceMaxRetransmits, | |
} | |
tryOnce, err := peerConnection.CreateDataChannel("tryOnce", tryOnceConfig) | |
if err != nil { | |
panic(err) | |
} | |
tryOnceSent := 0 | |
// Register channel opening handling | |
tryOnce.OnOpen(func() { | |
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", tryOnce.Label(), tryOnce.ID()) | |
for range time.NewTicker(2 * time.Millisecond).C { | |
message := signal.RandSeq(50000) | |
// Send the message as text | |
sendErr := tryOnce.SendText(message) | |
tryOnceSent++ | |
if sendErr != nil { | |
panic(sendErr) | |
} | |
} | |
}) | |
///////////////////////////////////// | |
///////////////////////////////////// | |
///////////////////////////////////// | |
// Wait for the offer to be pasted | |
offer := webrtc.SessionDescription{} | |
signal.Decode(signal.MustReadStdin(), &offer) | |
// Set the remote SessionDescription | |
err = peerConnection.SetRemoteDescription(offer) | |
if err != nil { | |
panic(err) | |
} | |
// Create an 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) | |
} | |
// Output the answer in base64 so we can paste it in browser | |
fmt.Println(signal.Encode(answer)) | |
for range time.NewTicker(1 * time.Second).C { | |
fmt.Printf("ttlRecv: %v \n", ttlRecv) | |
fmt.Printf("relSent: %v \n", reliableSent) | |
fmt.Printf("relRecv: %v \n", reliableRecv) | |
fmt.Printf("tryOnceSent: %v \n", tryOnceSent) | |
} | |
// Block forever | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment