From ea89184a6ffdd0f6aa6ddde0b0affa2e52f1fbd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Tue, 16 Nov 2021 21:34:10 -0300 Subject: [PATCH] added o methods --- smqcli/lib.go | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/smqcli/lib.go b/smqcli/lib.go index f91e636..7541f1a 100644 --- a/smqcli/lib.go +++ b/smqcli/lib.go @@ -1,6 +1,7 @@ package smqcli import ( + "encoding/json" "errors" "fmt" "github.com/gorilla/websocket" @@ -33,6 +34,15 @@ type MQSub struct { H func(m *types.Msg) } +func (c *MQConn) Write(m *types.Msg) error { + c.mtx.Lock() + defer c.mtx.Unlock() + if c.conn != nil { + return c.conn.WriteJSON(m) + } else { + return errors.New("c.conn is nil") + } +} func (c *MQConn) Sub(s string, h func(m *types.Msg)) error { //dbg("MQConn.Sub:: TOPIC:%s", s) m := &types.Msg{ @@ -60,17 +70,6 @@ func (c *MQConn) UnSub(s string) error { c.subs.Delete(s) return c.Write(m) } - -func (c *MQConn) Write(m *types.Msg) error { - c.mtx.Lock() - defer c.mtx.Unlock() - if c.conn != nil { - return c.conn.WriteJSON(m) - } else { - return errors.New("c.conn is nil") - } -} - func (c *MQConn) Pub(s string, pl []byte) error { //dbg("MQConn.Pub:: TOPIC:%s", s) m := &types.Msg{ @@ -125,6 +124,34 @@ func (c *MQConn) RpcReply(msg *types.Msg, pli []byte) error { return err } +func (c *MQConn) PubO(s string, i interface{}) error { + bs, err := json.Marshal(i) + if err != nil { + return err + } + return c.Pub(s, bs) +} +func (c *MQConn) RpcO(s string, oi interface{}, oo interface{}) error { + bs, err := json.Marshal(oi) + if err != nil { + return err + } + bs, err = c.Rpc(s, bs) + if err != nil { + return err + } + err = json.Unmarshal(bs, oo) + return err + +} +func (c *MQConn) RpcReplyO(msg *types.Msg, i interface{}) error { + bs, err := json.Marshal(i) + if err != nil { + return err + } + return c.RpcReply(msg, bs) +} + func (c *MQConn) Loop() { nerr := 0 for c.conn != nil {