From 152bbd3f76ed58de442d75ea86aefc0f9bd845cb Mon Sep 17 00:00:00 2001 From: xxjwxc Date: Sun, 2 May 2021 19:47:06 +0800 Subject: [PATCH] new --- mynsq/mynsq.go | 35 +++++++++++++++++------------------ mywebsocket/mywebsocket.go | 22 +++++++++++++--------- weixin/backup/cache.go | 8 ++++---- weixin/base.go | 2 +- weixin/qrcode.go | 4 ++-- 5 files changed, 37 insertions(+), 34 deletions(-) diff --git a/mynsq/mynsq.go b/mynsq/mynsq.go index da6cfd7..ad59abb 100644 --- a/mynsq/mynsq.go +++ b/mynsq/mynsq.go @@ -1,11 +1,10 @@ package mynsq import ( - "data/config" "log" - "public/mylog" "github.com/nsqio/go-nsq" + "github.com/xxjwxc/public/mylog" ) var producer *nsq.Producer = nil @@ -13,15 +12,15 @@ var consumerMap map[int]*nsq.Consumer = nil var cosumerTopics map[string][]int = nil func init() { - consumerMap = make(map[int]*nsq.Consumer) - cosumerTopics = make(map[string][]int) - cnf := nsq.NewConfig() - var err error - producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf) - if err != nil { - mylog.Error(err) - panic(err) - } + // consumerMap = make(map[int]*nsq.Consumer) + // cosumerTopics = make(map[string][]int) + // cnf := nsq.NewConfig() + // var err error + // producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf) + // if err != nil { + // mylog.Error(err) + // panic(err) + // } } @@ -30,13 +29,13 @@ func ProduceMsg(topic string, message []byte) bool { if producer == nil { //channel 锁住 - cnf := nsq.NewConfig() - var err error - producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf) - if err != nil { - mylog.Error(err) - return false - } + // cnf := nsq.NewConfig() + // var err error + // producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf) + // if err != nil { + // mylog.Error(err) + // return false + // } } if producer != nil { diff --git a/mywebsocket/mywebsocket.go b/mywebsocket/mywebsocket.go index 9b097cc..54449df 100644 --- a/mywebsocket/mywebsocket.go +++ b/mywebsocket/mywebsocket.go @@ -48,11 +48,12 @@ func WriteData(pathExp string, clientid string, body ClientBody) bool { } cache := mycache.NewCache("websocket" + pathExp) - tp, b := cache.Value(clientid) + var tp []*websocket.Conn + e := cache.Value(clientid, &tp) - if b { + if e == nil { b_r := false - tmp := tp.([]*websocket.Conn) + tmp := tp for i := 0; i < len(tmp); i++ { if _, err = tmp[i].Write(wb); err != nil { mylog.Debug("Can't send", err.Error()) @@ -108,9 +109,10 @@ func InitWebSocket(pathExp string, handlerFunc HandlerReadFunc, stateFunc Handle mutex.Lock() - tp, b := cache.Value(clientid) - if b && isMult { //多用户 - tmp = tp.([]*websocket.Conn) + var tp []*websocket.Conn + b := cache.Value(clientid, &tp) + if b == nil && isMult { //多用户 + tmp = tp } tmp = append(tmp, ws) @@ -174,9 +176,11 @@ func InitWebSocket(pathExp string, handlerFunc HandlerReadFunc, stateFunc Handle var tmp []*websocket.Conn mutex.Lock() - tp, b := cache.Value(clientid) - if b { - tmp = tp.([]*websocket.Conn) + + var tp []*websocket.Conn + b := cache.Value(clientid, &tp) + if b == nil { + tmp = tp } i := 0 for ; i < len(tmp); i++ { diff --git a/weixin/backup/cache.go b/weixin/backup/cache.go index 062dc63..149db6c 100644 --- a/weixin/backup/cache.go +++ b/weixin/backup/cache.go @@ -18,10 +18,10 @@ func NewGocache(server string) *Gocache { } //Get return cached value -func (mem *Gocache) Get(key string) interface{} { - v, _ := mem.mc.Value(key) - return v -} +// func (mem *Gocache) Get(key string) interface{} { +// v, _ := mem.mc.Value(key) +// return v +// } // IsExist check value exists in memcache. func (mem *Gocache) IsExist(key string) bool { diff --git a/weixin/base.go b/weixin/base.go index 640a500..db2a5df 100644 --- a/weixin/base.go +++ b/weixin/base.go @@ -144,7 +144,7 @@ func (_wx *wxTools) SendTemplateMsg(msg TempMsg) bool { } bo, _ := json.Marshal(msg) - resb := myhttp.OnPostJSON(_getSubscribe+accessToken, string(bo)) + resb, _ := myhttp.OnPostJSON(_getSubscribe+accessToken, string(bo)) var res ResTempMsg json.Unmarshal(resb, &res) diff --git a/weixin/qrcode.go b/weixin/qrcode.go index 9c0dd6f..a484596 100644 --- a/weixin/qrcode.go +++ b/weixin/qrcode.go @@ -22,7 +22,7 @@ func (_wx *wxTools) GetShareQrcode(path string, scene, page string) (ret QrcodeR accessToken, _ := _wx.GetAccessToken() // 获取access_token data := wxPostdata{Scene: scene, Page: page} bo, _ := json.Marshal(data) - resb := myhttp.OnPostJSON(GETSHAREURL+accessToken, string(bo)) + resb, _ := myhttp.OnPostJSON(GETSHAREURL+accessToken, string(bo)) tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制 if ret.Errcode == 0 { @@ -43,7 +43,7 @@ func (_wx *wxTools) GetWxQrcode(path, page string, width int) (ret QrcodeRet) { data := wxQrcodedata{Path: page, Width: width} bo, _ := json.Marshal(data) - resb := myhttp.OnPostJSON(GETQRCODEURL+accessToken, string(bo)) + resb, _ := myhttp.OnPostJSON(GETQRCODEURL+accessToken, string(bo)) tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制 if ret.Errcode == 0 {