mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-05-09 20:49:34 +08:00
message update
This commit is contained in:
parent
f517e63fca
commit
1ff8ba7fc2
@ -2,16 +2,15 @@ package new
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
promePkg "Open_IM/pkg/common/prometheus"
|
||||
"Open_IM/pkg/utils"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/envoyproxy/protoc-gen-validate/validate"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"open_im_sdk/pkg/log"
|
||||
"open_im_sdk/pkg/utils"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -50,6 +49,7 @@ type Client struct {
|
||||
encoder Encoder
|
||||
userContext UserConnContext
|
||||
validate *validator.Validate
|
||||
closed bool
|
||||
}
|
||||
|
||||
func newClient(conn LongConn, isCompress bool, userID string, isBackground bool, token string,
|
||||
@ -57,8 +57,7 @@ func newClient( conn LongConn,isCompress bool, userID string, isBackground boo
|
||||
return &Client{
|
||||
conn: conn,
|
||||
IsCompress: isCompress,
|
||||
userID: userID, IsBackground:
|
||||
isBackground, token: token,
|
||||
userID: userID, IsBackground: isBackground, token: token,
|
||||
connID: connID,
|
||||
onlineAt: onlineAt,
|
||||
handler: handler,
|
||||
@ -78,6 +77,9 @@ func(c *Client) readMessage(){
|
||||
if returnErr != nil {
|
||||
break
|
||||
}
|
||||
if c.closed == true {
|
||||
break
|
||||
}
|
||||
switch messageType {
|
||||
case PingMessage:
|
||||
case PongMessage:
|
||||
@ -137,12 +139,48 @@ func (c *Client) handleMessage(message []byte)error {
|
||||
default:
|
||||
return errors.New(fmt.Sprintf("ReqIdentifier failed,sendID:%d,msgIncr:%s,reqIdentifier:%s", binaryReq.SendID, binaryReq.MsgIncr, binaryReq.ReqIdentifier))
|
||||
}
|
||||
|
||||
c.replyMessage(binaryReq, messageErr, resp)
|
||||
return nil
|
||||
|
||||
}
|
||||
func (c *Client) close() {
|
||||
c.w.Lock()
|
||||
defer c.w.Unlock()
|
||||
c.conn.Close()
|
||||
c.unregisterChan <- c
|
||||
|
||||
}
|
||||
func () {
|
||||
|
||||
func (c *Client) replyMessage(binaryReq Req, err error, resp []byte) {
|
||||
mReply := Resp{
|
||||
ReqIdentifier: binaryReq.ReqIdentifier,
|
||||
MsgIncr: binaryReq.MsgIncr,
|
||||
OperationID: binaryReq.OperationID,
|
||||
Data: resp,
|
||||
}
|
||||
_ = c.writeMsg(mReply)
|
||||
}
|
||||
|
||||
func (c *Client) writeMsg(resp Resp) error {
|
||||
c.w.Lock()
|
||||
defer c.w.Unlock()
|
||||
if c.closed == true {
|
||||
return nil
|
||||
}
|
||||
encodedBuf := bufferPool.Get().([]byte)
|
||||
resultBuf := bufferPool.Get().([]byte)
|
||||
encodeBuf, err := c.encoder.Encode(resp)
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
_ = c.conn.SetWriteTimeout(60)
|
||||
if c.IsCompress {
|
||||
var compressErr error
|
||||
resultBuf, compressErr = c.compressor.Compress(encodeBuf)
|
||||
if compressErr != nil {
|
||||
return utils.Wrap(compressErr, "")
|
||||
}
|
||||
return c.conn.WriteMessage(MessageBinary, resultBuf)
|
||||
} else {
|
||||
return c.conn.WriteMessage(MessageBinary, encodedBuf)
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,14 @@ type Req struct {
|
||||
MsgIncr string `json:"msgIncr" validate:"required"`
|
||||
Data []byte `json:"data"`
|
||||
}
|
||||
type Resp struct {
|
||||
ReqIdentifier int32 `json:"reqIdentifier"`
|
||||
MsgIncr string `json:"msgIncr"`
|
||||
OperationID string `json:"operationID"`
|
||||
ErrCode int32 `json:"errCode"`
|
||||
ErrMsg string `json:"errMsg"`
|
||||
Data []byte `json:"data"`
|
||||
}
|
||||
type MessageHandler interface {
|
||||
GetSeq(context context.Context, data Req) ([]byte, error)
|
||||
SendMessage(context context.Context, data Req) ([]byte, error)
|
||||
|
@ -1,14 +1,22 @@
|
||||
package new
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/gorilla/websocket"
|
||||
"net/http"
|
||||
"open_im_sdk/pkg/utils"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
var bufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, 1000)
|
||||
},
|
||||
}
|
||||
type LongConnServer interface {
|
||||
Run() error
|
||||
}
|
||||
@ -58,6 +66,41 @@ func newWsServer(opts ...Option) (*WsServer, error) {
|
||||
}, nil
|
||||
}
|
||||
func (ws *WsServer) Run() error {
|
||||
var client *Client
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case client = <-ws.registerChan:
|
||||
ws.registerClient(client)
|
||||
case client = <-h.unregisterChan:
|
||||
h.unregisterClient(client)
|
||||
case msg = <-h.readChan:
|
||||
h.messageHandler(msg)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (ws *WsServer) registerClient(client *Client) {
|
||||
var (
|
||||
ok bool
|
||||
cli *Client
|
||||
)
|
||||
|
||||
if cli, ok = h.clients.Get(client.key); ok == false {
|
||||
h.clients.Set(client.key, client)
|
||||
atomic.AddInt64(&h.onlineConnections, 1)
|
||||
fmt.Println("R在线用户数量:", h.onlineConnections)
|
||||
return
|
||||
}
|
||||
|
||||
if client.onlineAt > cli.onlineAt {
|
||||
h.clients.Set(client.key, client)
|
||||
h.close(cli)
|
||||
return
|
||||
}
|
||||
h.close(client)
|
||||
}
|
||||
http.HandleFunc("/", ws.wsHandler) //Get request from client to handle by wsHandler
|
||||
return http.ListenAndServe(":"+utils.IntToString(ws.port), nil) //Start listening
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user