improve websocket connection according to gorrila example

This commit is contained in:
Adrian Zürcher
2025-06-22 09:03:18 +02:00
parent b29e7a97b8
commit 64ad4e8b3e
5 changed files with 163 additions and 184 deletions

View File

@@ -6,7 +6,6 @@ import (
ws "artNet/websocket"
"fmt"
"path"
"time"
json_data "github.com/tecamino/tecamino-json_data"
"github.com/tecamino/tecamino-logger/logging"
@@ -90,67 +89,33 @@ func (d *ArtNetDriver) SetValue(bus string, address uint, value uint8) error {
// id: id of driver
// port: port of server
func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
var err error
errChan := make(chan error)
client, err := ws.NewClient(ip, id, port)
if err != nil {
return err
}
client.OnError = func(err error) {
d.Log.Error("websocket connection", err)
}
client.OnMessage = func(data []byte) {
//fmt.Println(100, string(data))
fmt.Println(100, string(data))
errChan <- err
}
client.Connect(5)
client.OnMessage = func(data []byte) {
fmt.Println(100, string(data))
}
req := json_data.NewRequest()
req.AddDriverSubscription(".*", id, 0, true, false, false)
if err := client.SendData(req); err != nil {
if err := client.SendRequest(req); err != nil {
errChan <- err
d.Log.Error("websocket send data", err)
}
for {
time.Sleep(1)
select {
case err := <-errChan:
return err
}
}
return nil
// d.Conn = websocket.NewClient()
// if err := d.Conn.Connect(ip, id, port); err != nil {
// return err
// }
// defer d.Conn.Disconnect()
// if err := d.Conn.Subscribe(id); err != nil {
// return err
// }
// Subscribe to websocket server
// func (c *Client) Subscribe(id string) error {
// req := json_data.NewRequest()
// req.AddDriverSubscription(".*", id, 0, true, false, false)
// if err := wsjson.Write(c.ctx, c.conn, req); err != nil {
// return err
// }
// return nil
// }
// for {
// respond, err := d.Conn.ReadJsonData()
// if err != nil {
// return err
// }
// d.Subscribe(respond.Subscribe...)
// for _, pub := range respond.Publish {
// if sub, ok := d.Subscriptions[pub.Uuid]; ok {
// if err := d.SetValue(sub.Bus, sub.Address, uint8(pub.Value.(float64))); err != nil {
// d.Log.Info("artNet.Connect", err.Error())
// }
// }
// }
// }
}