Last active
August 16, 2018 07:35
-
-
Save chertov/0bb22c4627509b79698ba9d1a8975869 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"sync" | |
"time" | |
"github.com/pions/webrtc/pkg/datachannel" | |
"github.com/pions/webrtc/pkg/ice" | |
"github.com/pions/webrtc" | |
"strconv" | |
) | |
func createClient(name string, offerCh chan webrtc.RTCSessionDescription, answerCh chan webrtc.RTCSessionDescription, master bool) { | |
peerConnection, err := webrtc.New(webrtc.RTCConfiguration{ | |
ICEServers: []webrtc.RTCICEServer{ { URLs: []string{"stun:stun.l.google.com:19302"}, }, }, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
peerConnection.OnICEConnectionStateChange = func(connectionState ice.ConnectionState) { | |
fmt.Printf("Connection State has changed %s \n", connectionState.String()) | |
} | |
datachannels := make([]*webrtc.RTCDataChannel, 0) | |
var dataChannelsLock sync.RWMutex | |
peerConnection.Ondatachannel = func(d *webrtc.RTCDataChannel) { | |
dataChannelsLock.Lock() | |
datachannels = append(datachannels, d) | |
dataChannelsLock.Unlock() | |
fmt.Printf("New DataChannel %s %d\n", d.Label, d.ID) | |
d.Lock() | |
defer d.Unlock() | |
d.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(), d.Label, string(p.Data)) | |
case *datachannel.PayloadBinary: | |
fmt.Printf("Message '%s' from DataChannel '%s' payload '% 02x'\n", p.PayloadType().String(), d.Label, p.Data) | |
default: | |
fmt.Printf("Message '%s' from DataChannel '%s' no payload \n", p.PayloadType().String(), d.Label) | |
} | |
} | |
} | |
if master { | |
offer, err :=peerConnection.CreateOffer(nil) | |
if err != nil { | |
panic(err) | |
} | |
// fmt.Println("", name, "webrtc offer ", offer.Sdp) | |
offerCh <- offer | |
answer := <- answerCh | |
if err := peerConnection.SetRemoteDescription(answer); err != nil { | |
panic(err) | |
} | |
} else { | |
offer := <- offerCh | |
if err := peerConnection.SetRemoteDescription(offer); err != nil { | |
panic(err) | |
} | |
answer, err := peerConnection.CreateAnswer(nil) | |
if err != nil { | |
panic(err) | |
} | |
//fmt.Println("", name, "webrtc answer ", answer.Sdp) | |
answerCh <- answer | |
} | |
fmt.Println("Random messages will now be sent to any connected DataChannels every 5 seconds") | |
counter := uint64(0) | |
for { | |
time.Sleep(5 * time.Second) | |
message := name + " " + strconv.FormatUint(counter, 10) | |
fmt.Printf("%s is sending '%s' \n", name, message) | |
dataChannelsLock.RLock() | |
for _, d := range datachannels { | |
err := d.Send(datachannel.PayloadString{Data: []byte(message)}) | |
if err != nil { | |
panic(err) | |
} | |
} | |
dataChannelsLock.RUnlock() | |
counter++ | |
} | |
} | |
func main() { | |
webrtc.RegisterDefaultCodecs() | |
offer := make(chan webrtc.RTCSessionDescription) | |
answer := make(chan webrtc.RTCSessionDescription) | |
go createClient("Bob", offer, answer, false) | |
createClient("Alice", offer, answer, true) | |
} |
This file contains hidden or 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
Tried to parse ICE candidate, but failed candidate:udpcandidate 0 udp 21296 176.59.37.29 52450 typ srflx raddr 192.168.43.220 rport 52450 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 1 udp 63408 176.59.37.29 52450 typ srflx raddr 192.168.43.220 rport 52450 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 0 udp 21296 176.59.37.29 52450 typ srflx raddr 192.168.43.220 rport 52450 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 1 udp 63408 176.59.37.29 52450 typ srflx raddr 192.168.43.220 rport 52450 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 0 udp 21296 176.59.37.29 52450 typ srflx raddr 192.168.43.220 rport 52450 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 1 udp 63408 176.59.37.29 52450 typ srflx raddr 192.168.43.220 rport 52450 generation 0 Connection State has changed Checking | |
Random messages will now be sent to any connected DataChannels every 5 seconds | |
Tried to parse ICE candidate, but failed candidate:udpcandidate 0 udp 7832 176.59.37.29 64667 typ srflx raddr 192.168.43.220 rport 64667 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 1 udp 23124 176.59.37.29 64667 typ srflx raddr 192.168.43.220 rport 64667 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 0 udp 7832 176.59.37.29 64667 typ srflx raddr 192.168.43.220 rport 64667 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 1 udp 23124 176.59.37.29 64667 typ srflx raddr 192.168.43.220 rport 64667 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 0 udp 7832 176.59.37.29 64667 typ srflx raddr 192.168.43.220 rport 64667 generation 0 Tried to parse ICE candidate, but failed candidate:udpcandidate 1 udp 23124 176.59.37.29 64667 typ srflx raddr 192.168.43.220 rport 64667 generation 0 Random messages will now be sent to any connected DataChannels every 5 seconds | |
Connection State has changed Checking | |
Connection State has changed Connected | |
Connection State has changed Connected | |
Alice is sending 'Alice 0' | |
Bob is sending 'Bob 0' | |
Alice is sending 'Alice 1' | |
Bob is sending 'Bob 1' | |
Connection State has changed Disconnected | |
Alice is sending 'Alice 2' | |
Bob is sending 'Bob 2' | |
Alice is sending 'Alice 3' | |
Bob is sending 'Bob 3' | |
Alice is sending 'Alice 4' | |
Bob is sending 'Bob 4' | |
Alice is sending 'Alice 5' | |
Bob is sending 'Bob 5' | |
Alice is sending 'Alice 6' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment