51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package webrtcconnection
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"git.sense-t.eu.org/ACE/ace/servers/qemuserver"
|
|
"github.com/pion/webrtc/v3"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func dataChannel(d *webrtc.DataChannel) {
|
|
d.OnOpen(func() {
|
|
for {
|
|
status := qemuserver.GetStatus().String()
|
|
currentTime := time.Now().UnixMilli()
|
|
|
|
b, err := json.Marshal(map[string]any{
|
|
"qemu_status": status,
|
|
"server_time": currentTime,
|
|
})
|
|
if err != nil {
|
|
logrus.Errorf(
|
|
"failed to parse to json on '%s-%d' with error: %v",
|
|
d.Label(), *d.ID(), err,
|
|
)
|
|
}
|
|
|
|
if err := d.Send(b); err != nil {
|
|
logrus.Errorf(
|
|
"failed to send qemu status to '%s-%d' with error: %v",
|
|
d.Label(), *d.ID(), err,
|
|
)
|
|
}
|
|
time.Sleep(time.Second)
|
|
}
|
|
})
|
|
d.OnMessage(func(msg webrtc.DataChannelMessage) {
|
|
logrus.Debugf("received %d bytes message from '%s-%d'", len(msg.Data), d.Label(), *d.ID())
|
|
if !msg.IsString {
|
|
return
|
|
}
|
|
if err := qemuserver.SendEvent(msg.Data); err != nil {
|
|
logrus.Errorf(
|
|
"cannot parse message from '%s-%d' to qemu controll event: %v",
|
|
d.Label(), *d.ID(), err,
|
|
)
|
|
}
|
|
})
|
|
}
|