50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package webrtcconnection
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (c *Connection) dataChannel(d *webrtc.DataChannel) {
|
|
d.OnOpen(func() {
|
|
for {
|
|
status := c.QEMU.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 := c.QEMU.SendEvent(msg.Data); err != nil {
|
|
logrus.Errorf(
|
|
"cannot parse message from '%s-%d' to qemu controll event: %v",
|
|
d.Label(), *d.ID(), err,
|
|
)
|
|
}
|
|
})
|
|
}
|