Browse Source

WsCommunicator eingebaut

test
Klaus Wendel 2 years ago
parent
commit
eff7131cb7
  1. 62
      nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

62
nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

@ -21,7 +21,7 @@ type GosmBuffer []byte
type GosmAdapter interface {
//Revalue(...interface{}) (interface{}, error)
Convert() (GosmAdapter, error)
//WsCommunicator(method string, ctx context.Context, conn *websocket.Conn) (GosmAdapter, error)
WsCommunicator(string, context.Context, *websocket.Conn, ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType)
}
func (r GosmContainer) Convert() (GosmAdapter, error) {
@ -41,31 +41,34 @@ func (r GosmBuffer) Convert() (i GosmAdapter, err error) {
return
}
/*
func (r GosmContainer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn) (GosmAdapter, error) {
switch method {
case "json":
WsjsonSend(ctx, conn)
case "bytes":
WsSend()
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")
panic("Wrong communicator method or invalid number of dosth-functions")
}
return GosmBuffer{}, nil
}
func (r GosmBuffer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn) (GosmAdapter, error) {
switch method {
case "json":
WsjsonReceive()
case "bytes":
WsReceive()
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")
panic("Wrong communicator method or invalid number of dosth-functions")
}
return GosmContainer{}, nil
}
*/
func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{}) (err error, receiveval interface{}) {
err = nil
@ -132,7 +135,7 @@ func WsSend(ctx context.Context, conn *websocket.Conn, sendval []byte, mts webso
return
}
func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func([]byte) []byte) (err error, receiveval []byte, mtr websocket.MessageType) {
func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval []byte, mtr websocket.MessageType) {
var sendval []byte
err = nil
@ -142,7 +145,7 @@ func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func([]byte) []b
return
}
if dosth != nil {
sendval = dosth(receiveval)
sendval = dosth(receiveval).([]byte)
} else {
sendval = []byte{} //It doesn't make sense to return unprocess receiveval
}
@ -155,3 +158,18 @@ func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func([]byte) []b
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