mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 16:55:37 +02:00
## Description We are attempting to hookup maitred to the API Maitred duties will be: - [ ] Hookup to the API - [ ] Wait for signal (from the API) to start Steam - [ ] Stop signal to stop the gaming session, clean up Steam... and maybe do the backup ## Summary by CodeRabbit - **New Features** - Introduced Docker-based deployment configurations for both the main and relay applications. - Added new API endpoints enabling real-time machine messaging and enhanced IoT operations. - Expanded database schema and actor types to support improved machine tracking. - **Improvements** - Enhanced real-time communication and relay management with streamlined room handling. - Upgraded dependencies, logging, and error handling for greater stability and performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com> Co-authored-by: Kristian Ollikainen <14197772+DatCaptainHorse@users.noreply.github.com>
75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package connections
|
|
|
|
import (
|
|
"github.com/pion/webrtc/v4"
|
|
"google.golang.org/protobuf/proto"
|
|
"log/slog"
|
|
gen "relay/internal/proto"
|
|
)
|
|
|
|
// NestriDataChannel is a custom data channel with callbacks
|
|
type NestriDataChannel struct {
|
|
*webrtc.DataChannel
|
|
callbacks map[string]OnMessageCallback // MessageBase type -> callback
|
|
}
|
|
|
|
// NewNestriDataChannel creates a new NestriDataChannel from *webrtc.DataChannel
|
|
func NewNestriDataChannel(dc *webrtc.DataChannel) *NestriDataChannel {
|
|
ndc := &NestriDataChannel{
|
|
DataChannel: dc,
|
|
callbacks: make(map[string]OnMessageCallback),
|
|
}
|
|
|
|
// Handler for incoming messages
|
|
ndc.OnMessage(func(msg webrtc.DataChannelMessage) {
|
|
// If string type message, ignore
|
|
if msg.IsString {
|
|
return
|
|
}
|
|
|
|
// Decode message
|
|
var base gen.ProtoMessageInput
|
|
if err := proto.Unmarshal(msg.Data, &base); err != nil {
|
|
slog.Error("failed to decode binary DataChannel message", "err", err)
|
|
return
|
|
}
|
|
|
|
// Handle message type callback
|
|
if callback, ok := ndc.callbacks["input"]; ok {
|
|
go callback(msg.Data)
|
|
} // TODO: Log unknown message type?
|
|
})
|
|
|
|
return ndc
|
|
}
|
|
|
|
// SendBinary sends a binary message to the data channel
|
|
func (ndc *NestriDataChannel) SendBinary(data []byte) error {
|
|
return ndc.Send(data)
|
|
}
|
|
|
|
// RegisterMessageCallback registers a callback for a given binary message type
|
|
func (ndc *NestriDataChannel) RegisterMessageCallback(msgType string, callback OnMessageCallback) {
|
|
if ndc.callbacks == nil {
|
|
ndc.callbacks = make(map[string]OnMessageCallback)
|
|
}
|
|
ndc.callbacks[msgType] = callback
|
|
}
|
|
|
|
// UnregisterMessageCallback removes the callback for a given binary message type
|
|
func (ndc *NestriDataChannel) UnregisterMessageCallback(msgType string) {
|
|
if ndc.callbacks != nil {
|
|
delete(ndc.callbacks, msgType)
|
|
}
|
|
}
|
|
|
|
// RegisterOnOpen registers a callback for the data channel opening
|
|
func (ndc *NestriDataChannel) RegisterOnOpen(callback func()) {
|
|
ndc.OnOpen(callback)
|
|
}
|
|
|
|
// RegisterOnClose registers a callback for the data channel closing
|
|
func (ndc *NestriDataChannel) RegisterOnClose(callback func()) {
|
|
ndc.OnClose(callback)
|
|
}
|