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) {
r := engine.Group("ws")
ws.Init(ctx, r, flags.Source())
}, func(s *httpx.Server) {
s.Port = flags.Options.HttpPort
})
}

@ -13,50 +13,70 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/jageros/hawox/contextx"
"github.com/jageros/hawox/wsc"
"log"
"net/http"
"os"
"unicode"
"wechat/types"
"wechat/view"
)
func main() {
uid := flag.String("phone", "", "输入手机号码参数")
flag.Parse()
if *uid == "" {
log.Fatal("请携带手机号码参数启动,--phone=10086")
os.Exit(-1)
}
ctx, cancel := contextx.Default()
defer cancel()
var err error
h := http.Header{}
h.Add("uid", *uid)
var sess *wsc.Session
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 {
panic(err)
}
view.OnMessage("请发送本人手机号码加入聊天室!\n")
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 {
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) {
//uid, _ := session.Get("uid")
//roomId, _ := session.Get("roomId")
msg := &types.Msg{}
err := json.Unmarshal(bytes, msg)
msg, err := types.Unmarshal(bytes)
if err != nil {
log.Panicf("msg.Unmarshal err: %v", err)
view.OnMessage(fmt.Sprintf("解码错误:%v", err))
return
}
switch msg.MsgID {

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

Loading…
Cancel
Save