mirror of
https://github.com/pion/webrtc.git
synced 2025-10-06 15:46:54 +08:00
DataChannels: OpenChannel & OnOpen
- OpenChannel messages are now sent after SCTP is Established. - The OnOpen handler tells the application when a channel has opened. Relates to #159
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
@@ -34,22 +35,25 @@ func main() {
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peerConnection.OnICEConnectionStateChange = func(connectionState ice.ConnectionState) {
|
||||
fmt.Printf("Connection State has changed %s \n", connectionState.String())
|
||||
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
|
||||
}
|
||||
|
||||
// TODO: find the correct place for this
|
||||
if connectionState == ice.ConnectionStateConnected {
|
||||
time.AfterFunc(3*time.Second, func() {
|
||||
fmt.Println("sending openchannel")
|
||||
err := dataChannel.SendOpenChannelMessage()
|
||||
if err != nil {
|
||||
fmt.Println("faild to send openchannel", err)
|
||||
}
|
||||
})
|
||||
dataChannel.Lock()
|
||||
|
||||
// Register channel opening handling
|
||||
dataChannel.OnOpen = func() {
|
||||
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 {
|
||||
time.Sleep(5 * time.Second)
|
||||
message := randSeq(15)
|
||||
fmt.Printf("Sending %s \n", message)
|
||||
|
||||
err := dataChannel.Send(datachannel.PayloadString{Data: []byte(message)})
|
||||
check(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Register the Onmessage to handle incoming messages
|
||||
dataChannel.Lock()
|
||||
dataChannel.Onmessage = func(payload datachannel.Payload) {
|
||||
switch p := payload.(type) {
|
||||
case *datachannel.PayloadString:
|
||||
@@ -60,6 +64,7 @@ func main() {
|
||||
fmt.Printf("Message '%s' from DataChannel '%s' no payload \n", p.PayloadType().String(), dataChannel.Label)
|
||||
}
|
||||
}
|
||||
|
||||
dataChannel.Unlock()
|
||||
|
||||
// Create an offer to send to the browser
|
||||
@@ -69,7 +74,7 @@ func main() {
|
||||
// Output the offer in base64 so we can paste it in browser
|
||||
fmt.Println(base64.StdEncoding.EncodeToString([]byte(offer.Sdp)))
|
||||
|
||||
// Wait for the offer to be pasted
|
||||
// Wait for the answer to be pasted
|
||||
sd := mustReadStdin()
|
||||
|
||||
// Set the remote SessionDescription
|
||||
@@ -82,16 +87,8 @@ func main() {
|
||||
err = peerConnection.SetRemoteDescription(answer)
|
||||
check(err)
|
||||
|
||||
// Send messages every 5 seconds
|
||||
fmt.Println("Random messages will now be sent to any connected DataChannels every 5 seconds")
|
||||
for {
|
||||
time.Sleep(5 * time.Second)
|
||||
message := randSeq(15)
|
||||
fmt.Printf("Sending %s \n", message)
|
||||
|
||||
err := dataChannel.Send(datachannel.PayloadString{Data: []byte(message)})
|
||||
check(err)
|
||||
}
|
||||
// Block forever
|
||||
select {}
|
||||
}
|
||||
|
||||
// randSeq is used to generate a random message
|
||||
@@ -109,12 +106,15 @@ func randSeq(n int) string {
|
||||
func mustReadStdin() string {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
rawSd, err := reader.ReadString('\n')
|
||||
check(err)
|
||||
if err != io.EOF {
|
||||
check(err)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
sd, err := base64.StdEncoding.DecodeString(rawSd)
|
||||
|
||||
sd, err := base64.StdEncoding.DecodeString(rawSd)
|
||||
check(err)
|
||||
|
||||
return string(sd)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user