mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-10-27 22:12:15 +08:00
Merge branch 'openimsdk:main' into main
This commit is contained in:
commit
18cc83cab8
@ -20,6 +20,7 @@ import (
|
|||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||||||
"github.com/openimsdk/protocol/constant"
|
"github.com/openimsdk/protocol/constant"
|
||||||
@ -72,6 +73,8 @@ type Client struct {
|
|||||||
closed atomic.Bool
|
closed atomic.Bool
|
||||||
closedErr error
|
closedErr error
|
||||||
token string
|
token string
|
||||||
|
hbCtx context.Context
|
||||||
|
hbCancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetClient updates the client's state with new connection and context information.
|
// ResetClient updates the client's state with new connection and context information.
|
||||||
@ -88,6 +91,7 @@ func (c *Client) ResetClient(ctx *UserConnContext, conn LongConn, longConnServer
|
|||||||
c.closed.Store(false)
|
c.closed.Store(false)
|
||||||
c.closedErr = nil
|
c.closedErr = nil
|
||||||
c.token = ctx.GetToken()
|
c.token = ctx.GetToken()
|
||||||
|
c.hbCtx, c.hbCancel = context.WithCancel(c.ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) pingHandler(_ string) error {
|
func (c *Client) pingHandler(_ string) error {
|
||||||
@ -98,6 +102,13 @@ func (c *Client) pingHandler(_ string) error {
|
|||||||
return c.writePongMsg()
|
return c.writePongMsg()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) pongHandler(_ string) error {
|
||||||
|
if err := c.conn.SetReadDeadline(pongWait); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// readMessage continuously reads messages from the connection.
|
// readMessage continuously reads messages from the connection.
|
||||||
func (c *Client) readMessage() {
|
func (c *Client) readMessage() {
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -110,7 +121,9 @@ func (c *Client) readMessage() {
|
|||||||
|
|
||||||
c.conn.SetReadLimit(maxMessageSize)
|
c.conn.SetReadLimit(maxMessageSize)
|
||||||
_ = c.conn.SetReadDeadline(pongWait)
|
_ = c.conn.SetReadDeadline(pongWait)
|
||||||
|
c.conn.SetPongHandler(c.pongHandler)
|
||||||
c.conn.SetPingHandler(c.pingHandler)
|
c.conn.SetPingHandler(c.pingHandler)
|
||||||
|
c.activeHeartbeat(c.hbCtx)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
log.ZDebug(c.ctx, "readMessage")
|
log.ZDebug(c.ctx, "readMessage")
|
||||||
@ -147,6 +160,7 @@ func (c *Client) readMessage() {
|
|||||||
case CloseMessage:
|
case CloseMessage:
|
||||||
c.closedErr = ErrClientClosed
|
c.closedErr = ErrClientClosed
|
||||||
return
|
return
|
||||||
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,6 +249,7 @@ func (c *Client) close() {
|
|||||||
|
|
||||||
c.closed.Store(true)
|
c.closed.Store(true)
|
||||||
c.conn.Close()
|
c.conn.Close()
|
||||||
|
c.hbCancel() // Close server-initiated heartbeat.
|
||||||
c.longConnServer.UnRegister(c)
|
c.longConnServer.UnRegister(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,6 +336,44 @@ func (c *Client) writeBinaryMsg(resp Resp) error {
|
|||||||
return c.conn.WriteMessage(MessageBinary, encodedBuf)
|
return c.conn.WriteMessage(MessageBinary, encodedBuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actively initiate Heartbeat when platform in Web.
|
||||||
|
func (c *Client) activeHeartbeat(ctx context.Context) {
|
||||||
|
if c.PlatformID == constant.WebPlatformID {
|
||||||
|
go func() {
|
||||||
|
log.ZDebug(ctx, "server initiative send heartbeat start.")
|
||||||
|
ticker := time.NewTicker(pingPeriod)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
if err := c.writePingMsg(); err != nil {
|
||||||
|
log.ZWarn(c.ctx, "send Ping Message error.", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case <-c.hbCtx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (c *Client) writePingMsg() error {
|
||||||
|
if c.closed.Load() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c.w.Lock()
|
||||||
|
defer c.w.Unlock()
|
||||||
|
|
||||||
|
err := c.conn.SetWriteDeadline(writeWait)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.conn.WriteMessage(PingMessage, nil)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) writePongMsg() error {
|
func (c *Client) writePongMsg() error {
|
||||||
if c.closed.Load() {
|
if c.closed.Load() {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@ -53,6 +53,9 @@ const (
|
|||||||
// Time allowed to read the next pong message from the peer.
|
// Time allowed to read the next pong message from the peer.
|
||||||
pongWait = 30 * time.Second
|
pongWait = 30 * time.Second
|
||||||
|
|
||||||
|
// Send pings to peer with this period. Must be less than pongWait.
|
||||||
|
pingPeriod = (pongWait * 9) / 10
|
||||||
|
|
||||||
// Maximum message size allowed from peer.
|
// Maximum message size allowed from peer.
|
||||||
maxMessageSize = 51200
|
maxMessageSize = 51200
|
||||||
)
|
)
|
||||||
|
|||||||
@ -16,10 +16,11 @@ package msggateway
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/openimsdk/tools/apiresp"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/openimsdk/tools/apiresp"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/openimsdk/tools/errs"
|
"github.com/openimsdk/tools/errs"
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user