jager 3 years ago
parent afde513ad9
commit df9e8dee19

Binary file not shown.

Binary file not shown.

@ -27,5 +27,7 @@ func main() {
httpx.InitializeHttpServer(ctx, func(engine *gin.Engine) { httpx.InitializeHttpServer(ctx, func(engine *gin.Engine) {
r := engine.Group("ws") r := engine.Group("ws")
ws.Init(ctx, r, flags.Source()) ws.Init(ctx, r, flags.Source())
}, func(s *httpx.Server) {
s.Port = flags.Options.HttpPort
}) })
} }

@ -13,50 +13,70 @@
package main package main
import ( import (
"encoding/json" "fmt"
"flag"
"github.com/jageros/hawox/contextx" "github.com/jageros/hawox/contextx"
"github.com/jageros/hawox/wsc" "github.com/jageros/hawox/wsc"
"log" "log"
"net/http" "net/http"
"os" "unicode"
"wechat/types" "wechat/types"
"wechat/view" "wechat/view"
) )
func main() { func main() {
uid := flag.String("phone", "", "输入手机号码参数")
flag.Parse()
if *uid == "" {
log.Fatal("请携带手机号码参数启动,--phone=10086")
os.Exit(-1)
}
ctx, cancel := contextx.Default() ctx, cancel := contextx.Default()
defer cancel() defer cancel()
var err error
h := http.Header{} var sess *wsc.Session
h.Add("uid", *uid)
m := wsc.New(ctx) m := wsc.New(ctx)
sess, err := m.ConnectWithHeader("ws://127.0.0.1:8888/ws/wechat/1", h, map[string]interface{}{"uid": uid})
if err != nil { view.OnMessage("请发送本人手机号码加入聊天室!\n")
panic(err)
}
view.OnSendMsg(func(msg string) { view.OnSendMsg(func(msg string) {
err = sess.Write([]byte(msg)) if sess == nil {
if len(msg) != 11 {
view.OnMessage("手机号码格式错误!\n")
return
}
for _, r := range msg {
if !unicode.Is(unicode.Number, r) {
view.OnMessage("手机号码格式错误!\n")
return
}
}
uid := msg
h := http.Header{}
h.Add("uid", uid)
sess, err = m.ConnectWithHeader("ws://wechat.hawtech.cn/ws/wechat/1", h, map[string]interface{}{"uid": uid})
if err != nil {
view.OnMessage(fmt.Sprintf("创建websocket链接错误手机号码:%s 错误信息:%v\n", uid, err))
return
}
view.OnMessage("登录成功!\n")
return
}
data := &types.Msg{
MsgID: 1,
Msg: msg,
}
bytes, err := types.Marshal(data)
if err != nil { if err != nil {
log.Println(err) view.OnMessage(fmt.Sprintf("编码错误: %v", err))
return
}
err = sess.WriteBinary(bytes)
if err != nil {
} }
}) })
m.HandleMessageBinary(func(session *wsc.Session, bytes []byte) { m.HandleMessageBinary(func(session *wsc.Session, bytes []byte) {
//uid, _ := session.Get("uid") msg, err := types.Unmarshal(bytes)
//roomId, _ := session.Get("roomId")
msg := &types.Msg{}
err := json.Unmarshal(bytes, msg)
if err != nil { if err != nil {
log.Panicf("msg.Unmarshal err: %v", err) view.OnMessage(fmt.Sprintf("解码错误:%v", err))
return return
} }
switch msg.MsgID { switch msg.MsgID {

@ -12,8 +12,69 @@
package types package types
import (
"encoding/json"
"math/rand"
)
type Msg struct { type Msg struct {
MsgID int `json:"msg_id"` MsgID int `json:"msg_id"`
Seq int64 `json:"seq"` Seq int64 `json:"seq"`
Msg string `json:"msg"` Msg string `json:"msg"`
} }
type pkg struct {
N int
V1 []byte
V2 []byte
V3 []byte
}
func Marshal(msg *Msg) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, err
}
pk := &pkg{}
l := len(data)
for i := 0; i < l; i += 2 {
pk.N = 1
pk.V1 = append(pk.V1, data[i])
pk.V2 = append(pk.V2, uint8(rand.Intn(256)))
if i+1 < l {
pk.N = 2
pk.V3 = append(pk.V3, data[i+1])
}
}
return json.Marshal(pk)
}
func Unmarshal(data []byte) (*Msg, error) {
var pk = &pkg{}
err := json.Unmarshal(data, pk)
if err != nil {
return nil, err
}
var mds []byte
if pk.N == 1 {
l := len(pk.V3)
for i, v := range pk.V1 {
mds = append(mds, v)
if i < l {
mds = append(mds, pk.V3[i])
}
}
} else {
l := len(pk.V1)
for i, v := range pk.V3 {
if i < l {
mds = append(mds, pk.V1[i])
}
mds = append(mds, v)
}
}
res := &Msg{}
err = json.Unmarshal(mds, res)
return res, err
}

@ -13,7 +13,6 @@
package ws package ws
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jageros/hawox/contextx" "github.com/jageros/hawox/contextx"
@ -53,7 +52,7 @@ func Init(ctx contextx.Context, r *gin.RouterGroup, relativePath string) {
callTimeout: time.Second * 5, callTimeout: time.Second * 5,
online: map[string]struct{}{}, online: map[string]struct{}{},
} }
ss.m.HandleMessage(ss.handleMessage) ss.m.HandleMessageBinary(ss.handleMessage)
ss.m.HandleConnect(ss.onConnect) ss.m.HandleConnect(ss.onConnect)
ss.m.HandleDisconnect(ss.onDisconnect) ss.m.HandleDisconnect(ss.onDisconnect)
r.GET(relativePath, ss.handler) r.GET(relativePath, ss.handler)
@ -106,8 +105,9 @@ func (s *service) updateOnline() {
MsgID: 2, MsgID: 2,
Msg: msg, Msg: msg,
} }
bty, err := json.Marshal(resp) bty, err := types.Marshal(resp)
if err != nil { if err != nil {
logx.Errorf("编码错误:%v", err)
return return
} }
err = s.m.BroadcastBinary(bty) err = s.m.BroadcastBinary(bty)
@ -120,7 +120,6 @@ func (s *service) updateOnline() {
func (s *service) handleMessage(session *melody.Session, bytes []byte) { func (s *service) handleMessage(session *melody.Session, bytes []byte) {
start := time.Now() start := time.Now()
uid, exist := session.Get("uid") uid, exist := session.Get("uid")
logx.Infof("on msg uid=%s", uid)
if !exist { if !exist {
return return
} }
@ -131,16 +130,23 @@ func (s *service) handleMessage(session *melody.Session, bytes []byte) {
defer mux.Unlock() defer mux.Unlock()
seq += 1 seq += 1
msg := fmt.Sprintf("[%d]%s(%s): %s\n", seq, name, time.Now().Format("15:04:05"), string(bytes)) resp, err := types.Unmarshal(bytes)
if err != nil {
var resp = &types.Msg{ logx.Errorf("解码错误:%v", err)
MsgID: 1, return
Msg: msg,
} }
bty, err := json.Marshal(resp)
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)
if err != nil { if err != nil {
logx.Errorf("编码错误:%v", err)
return return
} }
err = s.m.BroadcastBinary(bty) err = s.m.BroadcastBinary(bty)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)

Loading…
Cancel
Save