This commit is contained in:
xxjwxc
2021-05-02 19:47:06 +08:00
parent 08a54c0722
commit 152bbd3f76
5 changed files with 37 additions and 34 deletions

View File

@@ -1,11 +1,10 @@
package mynsq package mynsq
import ( import (
"data/config"
"log" "log"
"public/mylog"
"github.com/nsqio/go-nsq" "github.com/nsqio/go-nsq"
"github.com/xxjwxc/public/mylog"
) )
var producer *nsq.Producer = nil var producer *nsq.Producer = nil
@@ -13,15 +12,15 @@ var consumerMap map[int]*nsq.Consumer = nil
var cosumerTopics map[string][]int = nil var cosumerTopics map[string][]int = nil
func init() { func init() {
consumerMap = make(map[int]*nsq.Consumer) // consumerMap = make(map[int]*nsq.Consumer)
cosumerTopics = make(map[string][]int) // cosumerTopics = make(map[string][]int)
cnf := nsq.NewConfig() // cnf := nsq.NewConfig()
var err error // var err error
producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf) // producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf)
if err != nil { // if err != nil {
mylog.Error(err) // mylog.Error(err)
panic(err) // panic(err)
} // }
} }
@@ -30,13 +29,13 @@ func ProduceMsg(topic string, message []byte) bool {
if producer == nil { if producer == nil {
//channel 锁住 //channel 锁住
cnf := nsq.NewConfig() // cnf := nsq.NewConfig()
var err error // var err error
producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf) // producer, err = nsq.NewProducer(config.GetNsqAddr(), cnf)
if err != nil { // if err != nil {
mylog.Error(err) // mylog.Error(err)
return false // return false
} // }
} }
if producer != nil { if producer != nil {

View File

@@ -48,11 +48,12 @@ func WriteData(pathExp string, clientid string, body ClientBody) bool {
} }
cache := mycache.NewCache("websocket" + pathExp) 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 b_r := false
tmp := tp.([]*websocket.Conn) tmp := tp
for i := 0; i < len(tmp); i++ { for i := 0; i < len(tmp); i++ {
if _, err = tmp[i].Write(wb); err != nil { if _, err = tmp[i].Write(wb); err != nil {
mylog.Debug("Can't send", err.Error()) mylog.Debug("Can't send", err.Error())
@@ -108,9 +109,10 @@ func InitWebSocket(pathExp string, handlerFunc HandlerReadFunc, stateFunc Handle
mutex.Lock() mutex.Lock()
tp, b := cache.Value(clientid) var tp []*websocket.Conn
if b && isMult { //多用户 b := cache.Value(clientid, &tp)
tmp = tp.([]*websocket.Conn) if b == nil && isMult { //多用户
tmp = tp
} }
tmp = append(tmp, ws) tmp = append(tmp, ws)
@@ -174,9 +176,11 @@ func InitWebSocket(pathExp string, handlerFunc HandlerReadFunc, stateFunc Handle
var tmp []*websocket.Conn var tmp []*websocket.Conn
mutex.Lock() mutex.Lock()
tp, b := cache.Value(clientid)
if b { var tp []*websocket.Conn
tmp = tp.([]*websocket.Conn) b := cache.Value(clientid, &tp)
if b == nil {
tmp = tp
} }
i := 0 i := 0
for ; i < len(tmp); i++ { for ; i < len(tmp); i++ {

View File

@@ -18,10 +18,10 @@ func NewGocache(server string) *Gocache {
} }
//Get return cached value //Get return cached value
func (mem *Gocache) Get(key string) interface{} { // func (mem *Gocache) Get(key string) interface{} {
v, _ := mem.mc.Value(key) // v, _ := mem.mc.Value(key)
return v // return v
} // }
// IsExist check value exists in memcache. // IsExist check value exists in memcache.
func (mem *Gocache) IsExist(key string) bool { func (mem *Gocache) IsExist(key string) bool {

View File

@@ -144,7 +144,7 @@ func (_wx *wxTools) SendTemplateMsg(msg TempMsg) bool {
} }
bo, _ := json.Marshal(msg) bo, _ := json.Marshal(msg)
resb := myhttp.OnPostJSON(_getSubscribe+accessToken, string(bo)) resb, _ := myhttp.OnPostJSON(_getSubscribe+accessToken, string(bo))
var res ResTempMsg var res ResTempMsg
json.Unmarshal(resb, &res) json.Unmarshal(resb, &res)

View File

@@ -22,7 +22,7 @@ func (_wx *wxTools) GetShareQrcode(path string, scene, page string) (ret QrcodeR
accessToken, _ := _wx.GetAccessToken() // 获取access_token accessToken, _ := _wx.GetAccessToken() // 获取access_token
data := wxPostdata{Scene: scene, Page: page} data := wxPostdata{Scene: scene, Page: page}
bo, _ := json.Marshal(data) bo, _ := json.Marshal(data)
resb := myhttp.OnPostJSON(GETSHAREURL+accessToken, string(bo)) resb, _ := myhttp.OnPostJSON(GETSHAREURL+accessToken, string(bo))
tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制 tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制
if ret.Errcode == 0 { 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} data := wxQrcodedata{Path: page, Width: width}
bo, _ := json.Marshal(data) bo, _ := json.Marshal(data)
resb := myhttp.OnPostJSON(GETQRCODEURL+accessToken, string(bo)) resb, _ := myhttp.OnPostJSON(GETQRCODEURL+accessToken, string(bo))
tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制 tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制
if ret.Errcode == 0 { if ret.Errcode == 0 {