mirror of
https://github.com/pion/webrtc.git
synced 2025-10-05 07:06:51 +08:00
Add vnet to SettingEngine
Also add simple example to show usage Resolves #778
This commit is contained in:
78
examples/vnet/show-network-usage/main.go
Normal file
78
examples/vnet/show-network-usage/main.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/pion/logging"
|
||||
"github.com/pion/transport/vnet"
|
||||
"github.com/pion/webrtc/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
wan, err := vnet.NewRouter(&vnet.RouterConfig{
|
||||
CIDR: "1.2.3.0/24",
|
||||
LoggerFactory: logging.NewDefaultLoggerFactory(),
|
||||
})
|
||||
panicIfError(err)
|
||||
|
||||
offerVNet := vnet.NewNet(&vnet.NetConfig{})
|
||||
panicIfError(wan.AddNet(offerVNet))
|
||||
|
||||
offerSettingEngine := webrtc.SettingEngine{}
|
||||
offerSettingEngine.SetVNet(offerVNet)
|
||||
offerAPI := webrtc.NewAPI(webrtc.WithSettingEngine(offerSettingEngine))
|
||||
|
||||
answerVNet := vnet.NewNet(&vnet.NetConfig{})
|
||||
panicIfError(wan.AddNet(answerVNet))
|
||||
|
||||
answerSettingEngine := webrtc.SettingEngine{}
|
||||
answerSettingEngine.SetVNet(answerVNet)
|
||||
answerAPI := webrtc.NewAPI(webrtc.WithSettingEngine(answerSettingEngine))
|
||||
|
||||
panicIfError(wan.Start())
|
||||
|
||||
offerPeerConnection, err := offerAPI.NewPeerConnection(webrtc.Configuration{})
|
||||
panicIfError(err)
|
||||
|
||||
answerPeerConnection, err := answerAPI.NewPeerConnection(webrtc.Configuration{})
|
||||
panicIfError(err)
|
||||
|
||||
offerDataChannel, err := offerPeerConnection.CreateDataChannel("label", nil)
|
||||
panicIfError(err)
|
||||
|
||||
msgSendLoop := func(dc *webrtc.DataChannel) {
|
||||
for {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
panicIfError(dc.SendText("My DataChannel Message"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
offerDataChannel.OnOpen(func() {
|
||||
msgSendLoop(offerDataChannel)
|
||||
})
|
||||
|
||||
answerPeerConnection.OnDataChannel(func(answerDataChannel *webrtc.DataChannel) {
|
||||
answerDataChannel.OnOpen(func() {
|
||||
msgSendLoop(answerDataChannel)
|
||||
})
|
||||
})
|
||||
|
||||
offer, err := offerPeerConnection.CreateOffer(nil)
|
||||
panicIfError(err)
|
||||
panicIfError(offerPeerConnection.SetLocalDescription(offer))
|
||||
panicIfError(answerPeerConnection.SetRemoteDescription(offer))
|
||||
|
||||
answer, err := answerPeerConnection.CreateAnswer(nil)
|
||||
panicIfError(err)
|
||||
panicIfError(answerPeerConnection.SetLocalDescription(answer))
|
||||
panicIfError(offerPeerConnection.SetRemoteDescription(answer))
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
func panicIfError(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user