improve websocket connection according to gorrila example
This commit is contained in:
@@ -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())
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
@@ -105,7 +105,7 @@ func (d *ArtNetDriver) Start(c *gin.Context) {
|
||||
}
|
||||
|
||||
busPayload := models.Bus{}
|
||||
if busPayload.ParsePayload(c); err != nil {
|
||||
if err := busPayload.ParsePayload(c); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user