From d74fc2ca7e6b003595dbd79256c962be0f32f289 Mon Sep 17 00:00:00 2001 From: Cody Baker Date: Sun, 22 Dec 2024 10:47:42 -0500 Subject: [PATCH] websocket support --- pkg/discovery/device.go | 11 +++++++++++ pkg/discovery/discovery.go | 1 + 2 files changed, 12 insertions(+) diff --git a/pkg/discovery/device.go b/pkg/discovery/device.go index e488577..0b6e114 100644 --- a/pkg/discovery/device.go +++ b/pkg/discovery/device.go @@ -3,6 +3,7 @@ package discovery import ( "context" "fmt" + "strings" "time" mqtt "github.com/eclipse/paho.mqtt.golang" @@ -47,6 +48,16 @@ func (d *Device) Open(ctx context.Context) (mgrpc.MgRPC, error) { } return mgrpc.Serve(ctx, c), nil } + if strings.HasPrefix(d.uri, "ws://") || strings.HasPrefix(d.uri, "wss://") { + c, err := mgrpc.New(ctx, d.uri, + mgrpc.UseWebSocket(), + ) + if err != nil { + return nil, fmt.Errorf("establishing rpc channel: %w", err) + } + ll.Info().Str("channel_protocol", "http").Msg("connected to device") + return c, nil + } c, err := mgrpc.New(ctx, d.uri, mgrpc.UseHTTPPost(), mgrpc.CodecOptions( diff --git a/pkg/discovery/discovery.go b/pkg/discovery/discovery.go index 8eb9ded..55797ea 100644 --- a/pkg/discovery/discovery.go +++ b/pkg/discovery/discovery.go @@ -81,6 +81,7 @@ func (d *Discoverer) AddDeviceByAddress(ctx context.Context, addr string, opts . } switch u.Scheme { case "http", "https": + case "ws", "wss": default: return nil, fmt.Errorf("unsupported URI scheme %q", u.Scheme) }