10 Star 39 Fork 19

Gitee 极速下载 / Pion-WebRTC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/pion/webrtc
克隆/下载
main.go 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
backkem 提交于 2018-09-25 22:04 . DataChannels: OpenChannel & OnOpen
package main
import (
"bufio"
"encoding/base64"
"fmt"
"io"
"math/rand"
"os"
"time"
"github.com/pions/webrtc"
"github.com/pions/webrtc/pkg/datachannel"
"github.com/pions/webrtc/pkg/ice"
)
func main() {
// Prepare the configuration
config := webrtc.RTCConfiguration{
IceServers: []webrtc.RTCIceServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
// Create a new RTCPeerConnection
peerConnection, err := webrtc.New(config)
check(err)
// Create a datachannel with label 'data'
dataChannel, err := peerConnection.CreateDataChannel("data", nil)
check(err)
// 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("ICE Connection State has changed: %s\n", connectionState.String())
}
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.Onmessage = func(payload datachannel.Payload) {
switch p := payload.(type) {
case *datachannel.PayloadString:
fmt.Printf("Message '%s' from DataChannel '%s' payload '%s'\n", p.PayloadType().String(), dataChannel.Label, string(p.Data))
case *datachannel.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)
}
}
dataChannel.Unlock()
// Create an offer to send to the browser
offer, err := peerConnection.CreateOffer(nil)
check(err)
// Output the offer in base64 so we can paste it in browser
fmt.Println(base64.StdEncoding.EncodeToString([]byte(offer.Sdp)))
// Wait for the answer to be pasted
sd := mustReadStdin()
// Set the remote SessionDescription
answer := webrtc.RTCSessionDescription{
Type: webrtc.RTCSdpTypeAnswer,
Sdp: sd,
}
// Apply the answer as the remote description
err = peerConnection.SetRemoteDescription(answer)
check(err)
// Block forever
select {}
}
// randSeq is used to generate a random message
func randSeq(n int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[r.Intn(len(letters))]
}
return string(b)
}
// mustReadStdin blocks untill input is received from stdin
func mustReadStdin() string {
reader := bufio.NewReader(os.Stdin)
rawSd, err := reader.ReadString('\n')
if err != io.EOF {
check(err)
}
fmt.Println("")
sd, err := base64.StdEncoding.DecodeString(rawSd)
check(err)
return string(sd)
}
// check is used to panic in an error occurs.
func check(err error) {
if err != nil {
panic(err)
}
}
1
https://gitee.com/mirrors/Pion-WebRTC.git
git@gitee.com:mirrors/Pion-WebRTC.git
mirrors
Pion-WebRTC
Pion-WebRTC
v1.1.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891