Browse Source

Das WsCommunication Framework, erste Version, ungetestet

test
Klaus Wendel 2 years ago
parent
commit
8256ef15a9
  1. 125
      nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

125
nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

@ -3,12 +3,12 @@ package nhooyrioWebsocketAddons
import (
"context"
"encoding/json"
"fmt"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
// GosmContainerSection is the type for one single information segment
type GosmContainerSection struct {
TagID string `json:"tagid"`
Attribute string `json:"attribute"` // might left empty
@ -17,40 +17,59 @@ type GosmContainerSection struct {
Status string `json:"status"` // reserved for status message; type errors is out of bounds
}
// GosmContainer is an accumulation of information segments
type GosmContainer []GosmContainerSection
// GosmBuffer is the GosmContainer as byte-array
type GosmBuffer []byte
//GosmAdapter is the master type for sending and receiving data between websocket client and server
type GosmAdapter interface {
//Revalue(...interface{}) (interface{}, error)
Revalue(func(interface{}) interface{})
Convert() (GosmAdapter, error)
WsCommunicator(string, context.Context, *websocket.Conn, ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType)
//WsCommunicator(string, context.Context, *websocket.Conn, ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType)
WsCommunicator(string, context.Context, *websocket.Conn, ...func(interface{}) interface{}) (error, websocket.MessageType)
}
func (r GosmContainer) Convert() (GosmAdapter, error) {
buffer, err := json.Marshal(r)
fmt.Println("Hier", r)
r = GosmContainer{GosmContainerSection{"E", "FF", "GGG", true, "Hallo"}}
fmt.Println("Hier2", r)
//Convert method turns GosmContainer-Data to GosmBuffer and reverse
func (r *GosmContainer) Convert() (GosmAdapter, error) {
buffer, err := json.Marshal(*r)
/* LÖSCHEN
fmt.Println("Hier", *r)
*r = GosmContainer{GosmContainerSection{"E", "FF", "GGG", true, "Hallo"}}
fmt.Println("Hier2", *r)*/
if err != nil {
panic(err)
}
return GosmBuffer(buffer), err
pbuffer := GosmBuffer(buffer)
return &pbuffer, err
}
func (r GosmBuffer) Convert() (i GosmAdapter, err error) {
//Convert method turns GosmContainer-Data to GosmBuffer and reverse
func (r *GosmBuffer) Convert() (GosmAdapter, error) {
var gosm GosmContainer
err = json.Unmarshal(r, &gosm)
i = gosm
return
err := json.Unmarshal(*r, &gosm)
return &gosm, err
}
//Revalue alters data
func (r *GosmContainer) Revalue(dosth func(interface{}) interface{}) {
*r = dosth(*r).(GosmContainer)
}
func (r GosmContainer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType) {
//Revalue alters data
func (r *GosmBuffer) Revalue(dosth func(interface{}) interface{}) {
//*r = dosth(*r).([]byte)
*r = dosth(*r).(GosmBuffer)
}
/*
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)
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])
@ -59,23 +78,65 @@ func (r GosmContainer) WsCommunicator(method string, ctx context.Context, conn *
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) {
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
err, receiveval := WsSend(ctx, conn, *r, websocket.MessageText)
pbuffer := GosmBuffer(receiveval)
return &pbuffer, err, websocket.MessageText
case method == "sendbinary" && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, r, websocket.MessageBinary)
return GosmBuffer(receiveval), err, websocket.MessageBinary
err, receiveval := WsSend(ctx, conn, *r, websocket.MessageBinary)
pbuffer := GosmBuffer(receiveval)
return &pbuffer, err, websocket.MessageBinary
case method == "receive" && len(dosth) == 1:
err, receiveval, mtr := WsReceive(ctx, conn, dosth[0])
return GosmBuffer(receiveval), err, mtr
pbuffer := GosmBuffer(receiveval)
return &pbuffer, err, mtr
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
}
*/
//WsCommunicator sends and receivs data to/from websocket partner
func (r *GosmContainer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (error, websocket.MessageType) {
switch {
case (method == "sendtext" || method == "sendbinary" || method == "send") && len(dosth) == 0:
err, receiveval := WsjsonSend(ctx, conn, *r)
*r = receiveval.(GosmContainer)
return err, websocket.MessageText
case method == "receive" && len(dosth) == 1:
err, receiveval := WsjsonReceive(ctx, conn, dosth[0])
*r = receiveval.(GosmContainer)
return err, websocket.MessageText
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
}
//WsCommunicator sends and receivs data to/from websocket partner
func (r *GosmBuffer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (error, websocket.MessageType) {
switch {
case (method == "sendtext" || method == "send") && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, *r, websocket.MessageText)
*r = GosmBuffer(receiveval)
return err, websocket.MessageText
case method == "sendbinary" && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, *r, websocket.MessageBinary)
*r = GosmBuffer(receiveval)
return err, websocket.MessageBinary
case method == "receive" && len(dosth) == 1:
err, receiveval, mtr := WsReceive(ctx, conn, dosth[0])
*r = GosmBuffer(receiveval)
return err, mtr
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
}
//WsjsonSend sends data to websocket partner in json text format
func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{}) (err error, receiveval interface{}) {
err = nil
@ -94,6 +155,7 @@ func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{})
return
}
//WsjsonReceive receives data from websocket partner in json text format
func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval interface{}) {
var sendval interface{}
err = nil
@ -104,7 +166,7 @@ func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interfa
return
}
if dosth != nil {
sendval = dosth(receiveval)
sendval = dosth(receiveval).(GosmContainer)
} else {
sendval = "" //It doesn't make sense to return unprocess receiveval
}
@ -118,6 +180,7 @@ func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interfa
return
}
//WsSend sends data to websocket partner as byte array
func WsSend(ctx context.Context, conn *websocket.Conn, sendval []byte, mts websocket.MessageType) (err error, receiveval []byte) {
var mtr websocket.MessageType
err = nil
@ -141,6 +204,7 @@ func WsSend(ctx context.Context, conn *websocket.Conn, sendval []byte, mts webso
return
}
//WsReceive receives data from websocket partner as byte array
func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval []byte, mtr websocket.MessageType) {
var sendval []byte
err = nil
@ -164,18 +228,3 @@ func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}
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
}
*/

Loading…
Cancel
Save