Compare commits

...

1 Commits

  1. 145
      nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

145
nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

@ -16,12 +16,14 @@ type CarrierJson struct {
Status string `json:"status"` // reserved for status message; type errors is out of bounds
}
type GosmStatus string
type GosmStatus string // Status of last transmission, and from/to which carrier is data relevant
const (
Upstream GosmStatus = "->"
Downstream GosmStatus = "<-"
Silent GosmStatus = "--"
UpstreamBuffer GosmStatus = "B>"
UpstreamJson GosmStatus = "J>"
DownstreamBuffer GosmStatus = "<B"
DownstreamJson GosmStatus = "<J"
Idle GosmStatus = "--"
)
type WsCommunicationMethod string
@ -40,8 +42,6 @@ type GosmContainer struct {
CarrierBuffer []byte
Status GosmStatus
Error error
Ctx context.Context
Conn *websocket.Conn
}
type GosmAdapter interface {
@ -51,8 +51,8 @@ type GosmAdapter interface {
//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{}, websocket.MessageType)
}
func (r *GosmContainer) RevalueJson(dosth func(CarrierJson) CarrierJson) {
@ -74,119 +74,52 @@ 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)
err, response = WsjsonSend(ctx, conn, (*r).CarrierJson)
(*r).Status = UpstreamJson
//(*r).CarrierJson = tmpreceiveval.(CarrierJson) //Überschreibe alten Wert?
case SendText:
err, (*r).CarrierBuffer = WsSend((*r).Ctx, (*r).Conn, (*r).CarrierBuffer, websocket.MessageText)
//err, (*r).CarrierBuffer = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageText)
err, response = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageText)
(*r).Status = UpstreamBuffer
case SendBinary:
err, (*r).CarrierBuffer = WsSend((*r).Ctx, (*r).Conn, (*r).CarrierBuffer, websocket.MessageBinary)
//err, (*r).CarrierBuffer = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageBinary)
err, response = WsSend(ctx, conn, (*r).CarrierBuffer, websocket.MessageBinary)
(*r).Status = UpstreamBuffer
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
}
*/
func (r *GosmContainer) WsContainerReceive(method WsCommunicationMethod, conn *websocket.Conn, ctx context.Context, dosth func(interface{}) interface{}) (err error, response interface{}, mtr websocket.MessageType) {
switch {
case method == ReceiveText || method == ReceiveBinary:
err, (*r).CarrierBuffer, response, mtr = WsReceive(ctx, conn, dosth)
(*r).Status = DownstreamBuffer
return err, response, mtr
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:
case method == ReceiveJson:
var tmpreceiveval interface{}
err, tmpreceiveval = WsjsonReceive((*r).Ctx, (*r).Conn, dosth)
(*r).CarrierJson = tmpreceiveval.(CarrierJson)
return err, websocket.MessageText
err, tmpreceiveval, response = WsjsonReceive(ctx, conn, dosth)
switch v := tmpreceiveval.(type) {
case CarrierJson:
(*r).CarrierJson = v
(*r).Status = DownstreamJson
default:
(*r).CarrierBuffer = []byte(v.(string))
(*r).Status = DownstreamBuffer
}
return err, response, 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{}) {
err = nil
@ -205,8 +138,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 +184,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(interface{}) interface{}) (err error, receiveval, sendval []byte, mtr websocket.MessageType) {
err = nil
//Receive

Loading…
Cancel
Save