10 Star 38 Fork 19

Gitee 极速下载 / Pion-WebRTC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/pion/webrtc
克隆/下载
main.go 3.21 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"math/rand"
"net/http"
"time"
"github.com/pions/webrtc"
"github.com/pions/webrtc/pkg/datachannel"
"github.com/pions/webrtc/pkg/ice"
)
func main() {
addr := flag.String("address", ":50000", "Address that the HTTP server is hosted on.")
flag.Parse()
// 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 range time.NewTicker(5 * time.Second).C {
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)
// Exchange the offer for the answer
answer := mustSignalViaHTTP(offer, *addr)
// Apply the answer as the remote description
err = peerConnection.SetRemoteDescription(answer)
check(err)
// Block forever
select {}
}
// mustSignalViaHTTP exchange the SDP offer and answer using an HTTP Post request.
func mustSignalViaHTTP(offer webrtc.RTCSessionDescription, address string) webrtc.RTCSessionDescription {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(offer)
check(err)
resp, err := http.Post("http://"+address, "application/json; charset=utf-8", b)
check(err)
defer resp.Body.Close()
var answer webrtc.RTCSessionDescription
err = json.NewDecoder(resp.Body).Decode(&answer)
check(err)
return answer
}
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)
}
// 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

搜索帮助