Skip to content

Commit

Permalink
Examples: Make examples/util internal
Browse files Browse the repository at this point in the history
Resolves #424
  • Loading branch information
backkem committed Feb 20, 2019
1 parent bf422e0 commit ddcef2d
Show file tree
Hide file tree
Showing 24 changed files with 542 additions and 247 deletions.
33 changes: 23 additions & 10 deletions examples/data-channels-close/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"time"

"github.com/pions/webrtc"
"github.com/pions/webrtc/examples/util"
"github.com/pions/webrtc/pkg/datachannel"

"github.com/pions/webrtc/examples/internal/signal"
)

func main() {
Expand All @@ -27,7 +28,9 @@ func main() {

// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
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
Expand All @@ -52,18 +55,22 @@ func main() {

cnt := *closeAfter
for range ticker.C {
message := util.RandSeq(15)
message := signal.RandSeq(15)
fmt.Printf("Sending %s \n", message)

err := d.Send(datachannel.PayloadString{Data: []byte(message)})
util.Check(err)
if err != nil {
panic(err)
}

cnt--
if cnt < 0 {
fmt.Printf("Sent %d times. Closing data channel '%s'-'%d'.\n", *closeAfter, d.Label, d.ID)
ticker.Stop()
err = d.Close()
util.Check(err)
if err != nil {
panic(err)
}
}
}
})
Expand All @@ -83,22 +90,28 @@ func main() {

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
util.Decode(util.MustReadStdin(), &offer)
signal.Decode(signal.MustReadStdin(), &offer)

// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
util.Check(err)
if err != nil {
panic(err)
}

// Create answer
answer, err := peerConnection.CreateAnswer(nil)
util.Check(err)
if err != nil {
panic(err)
}

// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(answer)
util.Check(err)
if err != nil {
panic(err)
}

// Output the answer in base64 so we can paste it in browser
fmt.Println(util.Encode(answer))
fmt.Println(signal.Encode(answer))

// Block forever
select {}
Expand Down
43 changes: 28 additions & 15 deletions examples/data-channels-create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"time"

"github.com/pions/webrtc"
"github.com/pions/webrtc/examples/util"
"github.com/pions/webrtc/pkg/datachannel"
sugar "github.com/pions/webrtc/pkg/datachannel"

"github.com/pions/webrtc/examples/internal/signal"
)

func main() {
Expand All @@ -23,11 +24,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
Expand All @@ -40,20 +45,22 @@ func main() {
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", dataChannel.Label, dataChannel.ID)

for range time.NewTicker(5 * time.Second).C {
message := util.RandSeq(15)
message := signal.RandSeq(15)
fmt.Printf("Sending %s \n", message)

err := dataChannel.Send(datachannel.PayloadString{Data: []byte(message)})
util.Check(err)
err := dataChannel.Send(sugar.PayloadString{Data: []byte(message)})
if err != nil {
panic(err)
}
}
})

// Register the OnMessage to handle incoming messages
dataChannel.OnMessage(func(payload datachannel.Payload) {
dataChannel.OnMessage(func(payload sugar.Payload) {
switch p := payload.(type) {
case *datachannel.PayloadString:
case *sugar.PayloadString:
fmt.Printf("Message '%s' from DataChannel '%s' payload '%s'\n", p.PayloadType().String(), dataChannel.Label, string(p.Data))
case *datachannel.PayloadBinary:
case *sugar.PayloadBinary:
fmt.Printf("Message '%s' from DataChannel '%s' payload '% 02x'\n", p.PayloadType().String(), dataChannel.Label, p.Data)
default:
fmt.Printf("Message '%s' from DataChannel '%s' no payload \n", p.PayloadType().String(), dataChannel.Label)
Expand All @@ -62,22 +69,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 {}
Expand Down
37 changes: 26 additions & 11 deletions examples/data-channels-detach-create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 {}
Expand All @@ -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)
}
}
}
33 changes: 23 additions & 10 deletions examples/data-channels-detach/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +31,9 @@ func main() {

// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
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
Expand All @@ -48,7 +51,9 @@ func main() {

// Detach the data channel
raw, dErr := d.Detach()
util.Check(dErr)
if dErr != nil {
panic(dErr)
}

// Handle reading from the data channel
go ReadLoop(raw)
Expand All @@ -60,22 +65,28 @@ func main() {

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
util.Decode(util.MustReadStdin(), offer)
signal.Decode(signal.MustReadStdin(), offer)

// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
util.Check(err)
if err != nil {
panic(err)
}

// Create answer
answer, err := peerConnection.CreateAnswer(nil)
util.Check(err)
if err != nil {
panic(err)
}

// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(answer)
util.Check(err)
if err != nil {
panic(err)
}

// Output the answer in base64 so we can paste it in browser
fmt.Println(util.Encode(answer))
fmt.Println(signal.Encode(answer))

// Block forever
select {}
Expand All @@ -98,10 +109,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)
}
}
}
Loading

0 comments on commit ddcef2d

Please sign in to comment.