mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-29 22:11:46 +08:00
gzip msg
This commit is contained in:
parent
9aaee04a69
commit
05d83ebe8b
@ -11,8 +11,10 @@ import (
|
|||||||
pbRelay "Open_IM/pkg/proto/relay"
|
pbRelay "Open_IM/pkg/proto/relay"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"context"
|
"context"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
go_redis "github.com/go-redis/redis/v8"
|
go_redis "github.com/go-redis/redis/v8"
|
||||||
@ -31,6 +33,7 @@ type UserConn struct {
|
|||||||
w *sync.Mutex
|
w *sync.Mutex
|
||||||
platformID int32
|
platformID int32
|
||||||
PushedMaxSeq uint32
|
PushedMaxSeq uint32
|
||||||
|
IsCompress bool
|
||||||
}
|
}
|
||||||
type WServer struct {
|
type WServer struct {
|
||||||
wsAddr string
|
wsAddr string
|
||||||
@ -75,7 +78,11 @@ func (ws *WServer) wsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Error(operationID, "upgrade http conn err", err.Error(), query)
|
log.Error(operationID, "upgrade http conn err", err.Error(), query)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
newConn := &UserConn{conn, new(sync.Mutex), utils.StringToInt32(query["platformID"][0]), 0}
|
var isCompress = false
|
||||||
|
if r.Header.Get("compression") == "gzip" {
|
||||||
|
isCompress = true
|
||||||
|
}
|
||||||
|
newConn := &UserConn{conn, new(sync.Mutex), utils.StringToInt32(query["platformID"][0]), 0, isCompress}
|
||||||
userCount++
|
userCount++
|
||||||
ws.addUserConn(query["sendID"][0], utils.StringToInt(query["platformID"][0]), newConn, query["token"][0], operationID)
|
ws.addUserConn(query["sendID"][0], utils.StringToInt(query["platformID"][0]), newConn, query["token"][0], operationID)
|
||||||
go ws.readMsg(newConn)
|
go ws.readMsg(newConn)
|
||||||
@ -97,6 +104,23 @@ func (ws *WServer) readMsg(conn *UserConn) {
|
|||||||
ws.delUserConn(conn)
|
ws.delUserConn(conn)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if conn.IsCompress {
|
||||||
|
buff := bytes.NewBuffer(msg)
|
||||||
|
reader, err := gzip.NewReader(buff)
|
||||||
|
if err != nil {
|
||||||
|
log.NewWarn("", "un gzip read failed")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
msg, err = ioutil.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
log.NewWarn("", "ReadAll failed")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = reader.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.NewWarn("", "reader close failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
ws.msgParse(conn, msg)
|
ws.msgParse(conn, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,6 +134,17 @@ func (ws *WServer) SetWriteTimeout(conn *UserConn, timeout int) {
|
|||||||
func (ws *WServer) writeMsg(conn *UserConn, a int, msg []byte) error {
|
func (ws *WServer) writeMsg(conn *UserConn, a int, msg []byte) error {
|
||||||
conn.w.Lock()
|
conn.w.Lock()
|
||||||
defer conn.w.Unlock()
|
defer conn.w.Unlock()
|
||||||
|
if conn.IsCompress {
|
||||||
|
buff := bytes.NewBuffer(msg)
|
||||||
|
gz := gzip.NewWriter(buff)
|
||||||
|
if _, err := gz.Write(buff.Bytes()); err != nil {
|
||||||
|
return utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
if err := gz.Close(); err != nil {
|
||||||
|
return utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
msg = buff.Bytes()
|
||||||
|
}
|
||||||
conn.SetWriteDeadline(time.Now().Add(time.Duration(60) * time.Second))
|
conn.SetWriteDeadline(time.Now().Add(time.Duration(60) * time.Second))
|
||||||
return conn.WriteMessage(a, msg)
|
return conn.WriteMessage(a, msg)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user