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.

175 lines
4.8 KiB

package nhooyrioWebsocketAddons
import (
"context"
"encoding/json"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
type GosmContainerSection struct {
TagID string `json:"tagid"`
Attribute string `json:"attribute"` // might left empty
Content string `json:"content"`
}
type GosmContainer []GosmContainerSection
type GosmBuffer []byte
type GosmAdapter interface {
//Revalue(...interface{}) (interface{}, error)
Convert() (GosmAdapter, error)
WsCommunicator(string, context.Context, *websocket.Conn, ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType)
}
func (r GosmContainer) Convert() (GosmAdapter, error) {
buffer, err := json.Marshal(r)
if err != nil {
panic(err)
}
return GosmBuffer(buffer), err
}
func (r GosmBuffer) Convert() (i GosmAdapter, err error) {
var gosm GosmContainer
err = json.Unmarshal(r, &gosm)
i = gosm
return
}
func (r GosmContainer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType) {
switch {
case (method == "sendtext" || method == "sendbinary" || method == "send") && len(dosth) == 0:
err, receiveval := WsjsonSend(ctx, conn, r)
return receiveval.(GosmAdapter), err, websocket.MessageText
case method == "receive" && len(dosth) == 1:
err, receiveval := WsjsonReceive(ctx, conn, dosth[0])
return receiveval.(GosmAdapter), err, websocket.MessageText
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
}
func (r GosmBuffer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType) {
switch {
case (method == "sendtext" || method == "send") && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, r, websocket.MessageText)
return GosmBuffer(receiveval), err, websocket.MessageText
case method == "sendbinary" && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, r, websocket.MessageBinary)
return GosmBuffer(receiveval), err, websocket.MessageBinary
case method == "receive" && len(dosth) == 1:
err, receiveval, mtr := WsReceive(ctx, conn, dosth[0])
return GosmBuffer(receiveval), err, mtr
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
}
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 interface{}) {
var 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(interface{}) interface{}) (err error, receiveval []byte, mtr websocket.MessageType) {
var sendval []byte
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
}
/*
func (r GosmContainer) Revalue(a ...interface{}) (i interface{}, err error) {
fmt.Println(r)
return
}
func (r GosmBuffer) Revalue(dosth func([]byte) []byte) (i interface{}, err error) {
fmt.Println(r)
r = []byte{91, 123, 34, 116, 97, 103, 105, 100, 34, 58, 34, 65, 34, 44, 34, 97, 116, 116, 114, 105, 98, 117, 116, 101, 34, 58, 34, 66, 66, 34, 44, 34, 99, 111, 110, 116, 101, 110, 116, 34, 58, 34, 67, 67, 67, 34, 125, 44, 123, 34, 116, 97, 103, 105, 100, 34, 58, 34, 68, 34, 44, 34, 97, 116, 116, 114, 105, 98, 117, 116, 101, 34, 58, 34, 69, 69, 34, 44, 34, 99, 111, 110, 116, 101, 110, 116, 34, 58, 34, 70, 70, 70, 34, 125, 93}
return
}
*/