Some tools, I frequently use.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.7 KiB

package nhooyrioWebsocketAddons
import (
"context"
"encoding/json"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
type CarrierJson struct {
TagID string `json:"tagid"`
Attribute string `json:"attribute"` // might left empty
Content string `json:"content"`
}
/*
type WsCommunicationMethod string
const (
SendTextMethod WsCommunicationMethod = "sendtext"
SendBinaryMethod WsCommunicationMethod = "sendbinary"
SendJsonMethod WsCommunicationMethod = "sendjson"
ReceiveTextMethod WsCommunicationMethod = "receivetext"
ReceiveBinaryMethod WsCommunicationMethod = "receivebinary"
ReceiveJsonMethod WsCommunicationMethod = "receivejson"
)
*/
type GosmContainer struct {
CarrierJson
CarrierBuffer []byte
}
type GosmAdapter interface {
RevalueJson(func(CarrierJson) CarrierJson)
ConvertJson2Buffer() error
ConvertBuffer2Json() error
//WsContainerSend(WsCommunicationMethod, *websocket.Conn, context.Context) (error, interface{})
//WsContainerReceive(WsCommunicationMethod, *websocket.Conn, context.Context, func(interface{}) interface{}) (error, interface{}, interface{}, websocket.MessageType)
}
func (r *GosmContainer) RevalueJson(dosth func(CarrierJson) CarrierJson) {
(*r).CarrierJson = dosth((*r).CarrierJson)
}
func (r *GosmContainer) ConvertJson2Buffer() (err error) {
(*r).CarrierBuffer, err = json.Marshal((*r).CarrierJson)
return
}
func (r *GosmContainer) ConvertBuffer2Json() (err error) {
var tmp CarrierJson
err = json.Unmarshal((*r).CarrierBuffer, &tmp)
(*r).CarrierJson = tmp
//err = json.Unmarshal((*r).CarrierBuffer, &((*r).CarrierJson))
return
}
/*
func (r *GosmContainer) WsContainerSend(method WsCommunicationMethod, conn *websocket.Conn, ctx context.Context) (err error, response interface{}) {
switch method {
case SendJsonMethod:
err, response = WsjsonSend(ctx, conn, (*r).CarrierJson)
//(*r).CarrierJson = tmpreceiveval.(CarrierJson) //Überschreibe alten Wert?
case SendTextMethod:
//err, (*r).CarrierBuffer = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageText)
err, response = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageText)
case SendBinaryMethod:
//err, (*r).CarrierBuffer = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageBinary)
err, response = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageBinary)
default:
err = errors.New("Wrong communicator method")
}
return
}
func (r *GosmContainer) WsContainerReceive(method WsCommunicationMethod, conn *websocket.Conn, ctx context.Context, dosth func(interface{}) interface{}) (err error, receiveval, sendval interface{}, mtr websocket.MessageType) {
switch {
case method == ReceiveTextMethod || method == ReceiveBinaryMethod:
err, receiveval, sendval, mtr = WsReceive(ctx, conn, dosth)
case method == ReceiveJsonMethod:
err, receiveval, sendval = WsjsonReceive(ctx, conn, dosth)
mtr = websocket.MessageText
default:
err = errors.New("Wrong communicator method")
}
return
}
*/
func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{}) (err error, receiveval interface{}) {
err = nil
//Send
err = wsjson.Write(ctx, conn, sendval)
if err != nil {
return err, nil
}
//Receive
err = wsjson.Read(ctx, conn, &receiveval)
if err != nil {
return err, nil
}
return
}
func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval, sendval interface{}) {
err = nil
//Receive
err = wsjson.Read(ctx, conn, &receiveval)
if err != nil {
return
}
if dosth != nil {
sendval = dosth(receiveval)
} else {
sendval = "" //It doesn't make sense to return unprocess receiveval
}
//Send
err = wsjson.Write(ctx, conn, sendval.(string))
if err != nil {
return
}
return
}
func WsSend(ctx context.Context, conn *websocket.Conn, sendval []byte, mts websocket.MessageType) (err error, receiveval []byte) {
var mtr websocket.MessageType
err = nil
//Send
err = conn.Write(ctx, mts, sendval)
if err != nil {
return err, nil
}
//Receive
mtr, receiveval, err = conn.Read(ctx)
if err != nil {
return err, nil
}
if mtr != mts { //Dürfte eigentlich niemals eintreten, denn MessageType mtr und mts sind identisch, sofern auf der anderen Seite WsReceive lauscht.
panic("websocket.MessageType inconsistency")
}
return
}
func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func([]byte) []byte) (err error, receiveval, sendval []byte, mtr websocket.MessageType) {
err = nil
//Receive
mtr, receiveval, err = conn.Read(ctx)
if err != nil {
return
}
if dosth != nil {
sendval = dosth(receiveval) //.([]byte)
} else {
sendval = []byte{} //It doesn't make sense to return unprocess receiveval
}
//Send
err = conn.Write(ctx, mtr, sendval)
if err != nil {
return
}
return
}