Safer Event Callbacks

Resolves #218

Change Event Callback APIs to setter functions which take care of
locking so that users don't need to know about or remember
to do this.
This commit is contained in:
Michael MacDonald
2018-11-07 10:01:54 -05:00
parent d3984899d1
commit d5cf800ebb
15 changed files with 420 additions and 131 deletions

View File

@@ -32,14 +32,12 @@ func main() {
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange = func(connectionState ice.ConnectionState) {
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() {
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)
@@ -49,10 +47,10 @@ func main() {
err := dataChannel.Send(datachannel.PayloadString{Data: []byte(message)})
util.Check(err)
}
}
})
// Register the Onmessage to handle incoming messages
dataChannel.Onmessage = func(payload datachannel.Payload) {
// 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))
@@ -61,9 +59,7 @@ func main() {
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)