added o methods

master
Paulo Simão 2021-11-16 21:34:10 -03:00
parent fecf1df963
commit ea89184a6f
1 changed files with 38 additions and 11 deletions

View File

@ -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 {