You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.1 KiB
Go

3 years ago
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: service
* @Date: 2021/7/8 5:30
* @package: service
* @Version: v1.0.0
*
* @Description:
*
*/
package ws
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jageros/hawox/contextx"
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpx"
"github.com/jageros/hawox/logx"
"gopkg.in/olahol/melody.v1"
"sync"
"time"
"wechat/types"
)
var (
seq int64
mux sync.Mutex
names = map[string]string{
"13160676597": "杰",
"13612225480": "文",
"13750043941": "哲",
}
)
var ss *service
type service struct {
ctx contextx.Context
m *melody.Melody
callTimeout time.Duration
3 years ago
online map[string]struct{}
mx sync.Mutex
3 years ago
}
func Init(ctx contextx.Context, r *gin.RouterGroup, relativePath string) {
ss = &service{
ctx: ctx,
m: melody.New(),
callTimeout: time.Second * 5,
3 years ago
online: map[string]struct{}{},
3 years ago
}
3 years ago
ss.m.HandleMessageBinary(ss.handleMessage)
3 years ago
ss.m.HandleConnect(ss.onConnect)
ss.m.HandleDisconnect(ss.onDisconnect)
r.GET(relativePath, ss.handler)
}
func (s *service) handler(c *gin.Context) {
uid := c.GetHeader("uid")
if _, ok := names[uid]; !ok {
3 years ago
logx.Infof("=====uid=%s", uid)
httpx.ErrInterrupt(c, errcode.InvalidParam.WithMsg(uid))
3 years ago
return
}
err := s.m.HandleRequestWithKeys(c.Writer, c.Request, map[string]interface{}{"uid": uid})
if err != nil {
httpx.ErrInterrupt(c, errcode.WithErrcode(-1, err))
}
}
func (s *service) onConnect(session *melody.Session) {
3 years ago
uid, exist := session.Get("uid")
logx.Infof("on connect uid=%s", uid)
if !exist {
return
}
s.mx.Lock()
defer s.mx.Unlock()
s.online[uid.(string)] = struct{}{}
s.updateOnline()
3 years ago
}
func (s *service) onDisconnect(session *melody.Session) {
3 years ago
uid, exist := session.Get("uid")
logx.Infof("on disconnect uid=%s", uid)
if !exist {
return
}
s.mx.Lock()
defer s.mx.Unlock()
delete(s.online, uid.(string))
s.updateOnline()
}
3 years ago
3 years ago
func (s *service) updateOnline() {
var msg string
for id := range s.online {
name := names[id]
msg = fmt.Sprintf("%s%s(%s)\n", msg, name, id)
}
var resp = &types.Msg{
MsgID: 2,
Msg: msg,
}
3 years ago
bty, err := types.Marshal(resp)
3 years ago
if err != nil {
3 years ago
logx.Errorf("编码错误:%v", err)
3 years ago
return
}
err = s.m.BroadcastBinary(bty)
if err != nil {
logx.Error(err)
}
logx.Infof("update %s", msg)
3 years ago
}
func (s *service) handleMessage(session *melody.Session, bytes []byte) {
start := time.Now()
uid, exist := session.Get("uid")
if !exist {
return
}
name := names[uid.(string)]
mux.Lock()
defer mux.Unlock()
seq += 1
3 years ago
resp, err := types.Unmarshal(bytes)
if err != nil {
logx.Errorf("解码错误:%v", err)
return
3 years ago
}
3 years ago
msg := fmt.Sprintf("[%d]%s(%s): %s\n", seq, name, time.Now().Format("15:04:05"), resp.Msg)
resp.Msg = msg
resp.MsgID = 1
resp.Seq = seq
bty, err := types.Marshal(resp)
3 years ago
if err != nil {
3 years ago
logx.Errorf("编码错误:%v", err)
3 years ago
return
}
3 years ago
3 years ago
err = s.m.BroadcastBinary(bty)
3 years ago
if err != nil {
logx.Error(err)
}
take := time.Now().Sub(start)
if take > time.Millisecond*100 {
logx.Warnf("send msg take: %s", take.String())
}
3 years ago
}