Created
April 20, 2019 15:43
-
-
Save chhsiao1981/e054f6263b1c1b147bbc35076a7f93f2 to your computer and use it in GitHub Desktop.
hang if the size of the read-bytes is less than the size of the write-bytes.
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" | |
"io" | |
"time" | |
"github.com/pion/webrtc/v2" | |
"github.com/pion/webrtc/v2/examples/internal/signal" | |
) | |
const messageSize = 15 | |
func main() { | |
// Since this behavior diverges from the WebRTC API it has to be | |
// enabled using a settings engine. Mixing both detached and the | |
// OnMessage DataChannel API is not supported. | |
// Create a SettingEngine and enable Detach | |
s := webrtc.SettingEngine{} | |
s.DetachDataChannels() | |
// Create an API object with the engine | |
api := webrtc.NewAPI(webrtc.WithSettingEngine(s)) | |
// 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 using the API object | |
peerConnection, err := api.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.\n", d.Label(), d.ID()) | |
// Detach the data channel | |
raw, dErr := d.Detach() | |
if dErr != nil { | |
panic(dErr) | |
} | |
// Handle reading from the data channel | |
go ReadLoop(raw) | |
// Handle writing to the data channel | |
go WriteLoop(raw) | |
}) | |
}) | |
// 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 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)) | |
// Block forever | |
select {} | |
} | |
// ReadLoop shows how to read from the datachannel directly | |
func ReadLoop(d io.Reader) { | |
for { | |
buffer := make([]byte, messageSize-1) | |
n, err := d.Read(buffer) | |
if err != nil { | |
fmt.Println("Datachannel closed; Exit the readloop:", err) | |
return | |
} | |
fmt.Printf("Message from DataChannel: %s\n", string(buffer[:n])) | |
} | |
} | |
// WriteLoop shows how to write to the datachannel directly | |
func WriteLoop(d io.Writer) { | |
for range time.NewTicker(5 * time.Second).C { | |
message := signal.RandSeq(messageSize) | |
fmt.Printf("Sending %s \n", message) | |
_, err := d.Write([]byte(message)) | |
if err != nil { | |
panic(err) | |
} | |
} | |
} |
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
diff --git a/examples/data-channels-detach/main.go b/examples/data-channels-detach/main.go | |
index 090dc52..c43e139 100644 | |
--- a/examples/data-channels-detach/main.go | |
+++ b/examples/data-channels-detach/main.go | |
@@ -101,7 +101,7 @@ func main() { | |
// ReadLoop shows how to read from the datachannel directly | |
func ReadLoop(d io.Reader) { | |
for { | |
- buffer := make([]byte, messageSize) | |
+ buffer := make([]byte, messageSize-1) | |
n, err := d.Read(buffer) | |
if err != nil { | |
fmt.Println("Datachannel closed; Exit the readloop:", err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment