Browse Source

WsContainerSend und WsContainerReceive waren Mist, abgehakt.

master
Klaus Wendel 2 years ago
parent
commit
ca77353df7
  1. 160
      nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

160
nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

@ -3,7 +3,6 @@ package nhooyrioWebsocketAddons
import (
"context"
"encoding/json"
"errors"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
@ -13,46 +12,32 @@ type CarrierJson struct {
TagID string `json:"tagid"`
Attribute string `json:"attribute"` // might left empty
Content string `json:"content"`
Status string `json:"status"` // reserved for status message; type errors is out of bounds
}
type GosmStatus string
const (
Upstream GosmStatus = "->"
Downstream GosmStatus = "<-"
Silent GosmStatus = "--"
)
/*
type WsCommunicationMethod string
const (
SendText WsCommunicationMethod = "sendtext"
SendBinary WsCommunicationMethod = "sendbinary"
SendJson WsCommunicationMethod = "sendjson"
ReceiveText WsCommunicationMethod = "receivetext"
ReceiveBinary WsCommunicationMethod = "receivebinary"
ReceiveJson WsCommunicationMethod = "receivejson"
SendTextMethod WsCommunicationMethod = "sendtext"
SendBinaryMethod WsCommunicationMethod = "sendbinary"
SendJsonMethod WsCommunicationMethod = "sendjson"
ReceiveTextMethod WsCommunicationMethod = "receivetext"
ReceiveBinaryMethod WsCommunicationMethod = "receivebinary"
ReceiveJsonMethod WsCommunicationMethod = "receivejson"
)
*/
type GosmContainer struct {
CarrierJson
CarrierBuffer []byte
Status GosmStatus
Error error
Ctx context.Context
Conn *websocket.Conn
}
type GosmAdapter interface {
RevalueJson(func(CarrierJson) CarrierJson)
ConvertJson2Buffer() error
ConvertBuffer2Json() error
//WsServerAccept
//WsServerClose
//WsClientDial
WsContainerSend(method WsCommunicationMethod) error
WsContainerReceive(method WsCommunicationMethod, dosth func(interface{}) interface{}) (error, websocket.MessageType)
//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) {
@ -74,117 +59,36 @@ func (r *GosmContainer) ConvertBuffer2Json() (err error) {
return
}
func (r *GosmContainer) WsContainerSend(method WsCommunicationMethod) (err error) {
/*
func (r *GosmContainer) WsContainerSend(method WsCommunicationMethod, conn *websocket.Conn, ctx context.Context) (err error, response interface{}) {
switch method {
case SendJson:
var tmpreceiveval interface{}
err, tmpreceiveval = WsjsonSend((*r).Ctx, (*r).Conn, (*r).CarrierJson)
(*r).CarrierJson = tmpreceiveval.(CarrierJson)
case SendText:
err, (*r).CarrierBuffer = WsSend((*r).Ctx, (*r).Conn, (*r).CarrierBuffer, websocket.MessageText)
case SendBinary:
err, (*r).CarrierBuffer = WsSend((*r).Ctx, (*r).Conn, (*r).CarrierBuffer, websocket.MessageBinary)
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, dosth func(interface{}) interface{}) (err error, mtr websocket.MessageType) {
/*
dosthI := func(i interface{}) (o interface{}) {
o = dosth(i.(CarrierJson))
return
}
*/
switch method {
case ReceiveText:
err, (*r).CarrierBuffer, mtr = WsReceive((*r).Ctx, (*r).Conn, dosth)
return err, mtr
case ReceiveBinary:
err, (*r).CarrierBuffer, mtr = WsReceive((*r).Ctx, (*r).Conn, dosth)
return err, mtr
case ReceiveJson:
var tmpreceiveval interface{}
err, tmpreceiveval = WsjsonReceive((*r).Ctx, (*r).Conn, dosth)
(*r).CarrierJson = tmpreceiveval.(CarrierJson)
return err, websocket.MessageText
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
}
/*
type GosmAtapder interface {
Revalue(func(interface{}) interface{})
Convert() (GosmAtapder, error)
//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 *GosmContratainer) Convert() (GosmAtapder, error) {
buffer, err := json.Marshal(*r)
if err != nil {
panic(err)
}
pbuffer := GosmBuffer(buffer)
return &pbuffer, err
}
func (r *GosmBuffer) Convert() (GosmAtapder, error) {
var gosm GosmContratainer
err := json.Unmarshal(*r, &gosm)
return &gosm, err
}
func (r *GosmContratainer) Revalue(dosth func(interface{}) interface{}) {
*r = dosth(*r).(GosmContratainer)
}
func (r *GosmBuffer) Revalue(dosth func(interface{}) interface{}) {
//*r = dosth(*r).([]byte)
*r = dosth(*r).(GosmBuffer)
}
func (r *GosmContratainer) 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.(GosmContratainer)
return err, websocket.MessageText
case method == "receive" && len(dosth) == 1:
err, receiveval := WsjsonReceive(ctx, conn, dosth[0])
*r = receiveval.(GosmContratainer)
return 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{}) (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")
}
}
*/
func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{}) (err error, receiveval interface{}) {
@ -205,8 +109,7 @@ func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{})
return
}
func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval interface{}) {
var sendval interface{}
func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval, sendval interface{}) {
err = nil
//Receive
@ -252,8 +155,7 @@ func WsSend(ctx context.Context, conn *websocket.Conn, sendval []byte, mts webso
return
}
func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval []byte, mtr websocket.MessageType) {
var sendval []byte
func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func([]byte) []byte) (err error, receiveval, sendval []byte, mtr websocket.MessageType) {
err = nil
//Receive
@ -262,7 +164,7 @@ func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}
return
}
if dosth != nil {
sendval = dosth(receiveval).([]byte)
sendval = dosth(receiveval) //.([]byte)
} else {
sendval = []byte{} //It doesn't make sense to return unprocess receiveval
}

Loading…
Cancel
Save