view
parent
b1353fc647
commit
67d44ea967
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @Author: jager
|
||||
* @Email: lhj168os@gmail.com
|
||||
* @File: types
|
||||
* @Date: 2021/10/20 5:03 下午
|
||||
* @package: types
|
||||
* @Version: v1.0.0
|
||||
*
|
||||
* @Description:
|
||||
*
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
type Msg struct {
|
||||
MsgID int `json:"msg_id"`
|
||||
Seq int64 `json:"seq"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
/**
|
||||
* @Author: jager
|
||||
* @Email: lhj168os@gmail.com
|
||||
* @File: editor
|
||||
* @Date: 2021/10/19 6:19 下午
|
||||
* @package: view
|
||||
* @Version: v1.0.0
|
||||
*
|
||||
* @Description:
|
||||
*
|
||||
*/
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/jroimartin/gocui"
|
||||
"github.com/mattn/go-runewidth"
|
||||
)
|
||||
|
||||
func modifyCJK(p []byte) []byte {
|
||||
buf := bytes.NewBuffer(bytes.Trim(p, " \n\t"))
|
||||
sz := len(buf.String())
|
||||
buf1 := bytes.NewBufferString("")
|
||||
var r rune
|
||||
var wr bool
|
||||
for i := 0; i < sz; i++ {
|
||||
r, _, _ = buf.ReadRune()
|
||||
if r != rune(0) && wr == false {
|
||||
buf1.WriteRune(r)
|
||||
} else if wr == true {
|
||||
if r != rune(' ') {
|
||||
buf1.WriteRune(r)
|
||||
}
|
||||
}
|
||||
wr = runewidth.RuneWidth(r) > 1
|
||||
}
|
||||
return buf1.Bytes()
|
||||
}
|
||||
|
||||
//ReadEditor Read byte array from editor 'v', delete the auto appended blank after CJK runes.
|
||||
func ReadEditor(v *gocui.View) []byte {
|
||||
var b = make([]byte, 300)
|
||||
n, _ := v.Read(b)
|
||||
if n > 0 {
|
||||
return modifyCJK(b[:n])
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @Author: jager
|
||||
* @Email: lhj168os@gmail.com
|
||||
* @File: service
|
||||
* @Date: 2021/7/8 5:30 下午
|
||||
* @package: service
|
||||
* @Version: v1.0.0
|
||||
*
|
||||
* @Description:
|
||||
*
|
||||
*/
|
||||
|
||||
package ws
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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
|
||||
}
|
||||
|
||||
func Init(ctx contextx.Context, r *gin.RouterGroup, relativePath string) {
|
||||
ss = &service{
|
||||
ctx: ctx,
|
||||
m: melody.New(),
|
||||
callTimeout: time.Second * 5,
|
||||
}
|
||||
ss.m.HandleMessageBinary(ss.handleMessage)
|
||||
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 {
|
||||
httpx.ErrInterrupt(c, errcode.InvalidParam)
|
||||
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) {
|
||||
|
||||
}
|
||||
|
||||
func (s *service) onDisconnect(session *melody.Session) {
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
msg := fmt.Sprintf("[%d]%s(%s): %s\n", seq, name, time.Now().Format("15:04:05"), string(bytes))
|
||||
|
||||
var resp = &types.Msg{
|
||||
MsgID: 1,
|
||||
Msg: msg,
|
||||
}
|
||||
bty, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = s.m.Broadcast(bty)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
}
|
||||
take := time.Now().Sub(start)
|
||||
if take > time.Millisecond*100 {
|
||||
logx.Warnf("send msg take: %s", take.String())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue