Browse Source

WsSend und WsReceive ([]byte) hinzugefügt

test
Klaus Wendel 2 years ago
parent
commit
9584959f23
  1. 52
      nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

52
nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

@ -8,6 +8,8 @@ import (
)
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 {
@ -20,8 +22,9 @@ func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{})
return err, nil
}
return nil, receiveval
return
}
func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval interface{}) {
var sendval interface{}
err = nil
@ -45,3 +48,50 @@ func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interfa
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([]byte) []byte) (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)
} else {
sendval = []byte{} //It doesn't make sense to return unprocess receiveval
}
//Send
err = conn.Write(ctx, mtr, sendval)
if err != nil {
return
}
return
}

Loading…
Cancel
Save