abstract write to websocket and add request id
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tecamino/tecamino-dbm/auth"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
@@ -35,31 +36,46 @@ func (d *DBMHandler) WebSocket(c *gin.Context) {
|
||||
|
||||
//Read loop
|
||||
for {
|
||||
|
||||
request, err := d.readJsonData(id)
|
||||
if err != nil {
|
||||
d.Log.Error("websocket.WebSocket", err.Error())
|
||||
break
|
||||
}
|
||||
|
||||
// Sets
|
||||
go d.Set(request.Set)
|
||||
|
||||
d.Get(request.Get, id, request.Id)
|
||||
// Sets
|
||||
d.Set(request.Set)
|
||||
|
||||
// Subscribe
|
||||
go d.Subscribe(request.Subscribe, id)
|
||||
d.Subscribe(request.Subscribe, id, request.Id)
|
||||
|
||||
// Unsubscribe
|
||||
go d.Unsubscribe(request.Unsubscribe, id)
|
||||
d.Unsubscribe(request.Unsubscribe, id)
|
||||
|
||||
request.Get = make([]json_dataModels.Get, 0)
|
||||
request.Set = make([]json_dataModels.Set, 0)
|
||||
request.Subscribe = make([]json_dataModels.Subscribe, 0)
|
||||
request.Unsubscribe = make([]json_dataModels.Subscribe, 0)
|
||||
request = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DBMHandler) readJsonData(id string) (request json_dataModels.Request, err error) {
|
||||
func (d *DBMHandler) readJsonData(id string) (request *json_dataModels.Request, err error) {
|
||||
|
||||
client, ok := d.Conns.Clients[id]
|
||||
if !ok {
|
||||
return request, errors.New("client id not found")
|
||||
}
|
||||
|
||||
err = wsjson.Read(client.Ctx, client.Conn, &request)
|
||||
_, reader, err := client.Conn.Reader(client.Ctx)
|
||||
if err != nil {
|
||||
return request, err
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
code := websocket.CloseStatus(err)
|
||||
|
||||
@@ -74,5 +90,10 @@ func (d *DBMHandler) readJsonData(id string) (request json_dataModels.Request, e
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(b, &request); err != nil {
|
||||
return request, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user