|
@@ -6,15 +6,13 @@ import (
|
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
- "github.com/google/martian/v3/log"
|
|
|
"github.com/gorilla/websocket"
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
"github.com/rotisserie/eris"
|
|
|
+ "go.uber.org/zap"
|
|
|
"sikey.com/websocket/models"
|
|
|
"sikey.com/websocket/repositories"
|
|
|
- "sikey.com/websocket/utils/keys"
|
|
|
"x.sikey.com.cn/serverx/gid"
|
|
|
- "x.sikey.com.cn/serverx/zlog"
|
|
|
)
|
|
|
|
|
|
type Client struct {
|
|
@@ -42,31 +40,31 @@ type Client struct {
|
|
|
|
|
|
func (c *Client) reader() {
|
|
|
defer func() {
|
|
|
+ zap.L().Info("client Offline", zap.String("user_id", c.UserId))
|
|
|
c.hub.Disconnect <- c
|
|
|
c.Close()
|
|
|
- zlog.With(keys.WebsocketClient, c.UserId).Info("client Offline")
|
|
|
}()
|
|
|
|
|
|
c.UnderlyingConn.SetReadDeadline(time.Now().Add(c.readWait))
|
|
|
for {
|
|
|
_, bytes, err := c.UnderlyingConn.ReadMessage()
|
|
|
if err != nil {
|
|
|
- if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
|
|
- zlog.With(keys.WebsocketClient, c.UserId).Errorf("error: %v", eris.Wrap(err, c.UserId))
|
|
|
- } else {
|
|
|
- zlog.With(keys.WebsocketClient, c.UserId).Errorf("error: %v", eris.Wrap(err, c.UserId))
|
|
|
- }
|
|
|
+ // if websocket.IsUnexpectedCloseError(err,
|
|
|
+ // websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
|
|
+ // log.Errorf("error: %v", eris.Wrap(err, c.UserId))
|
|
|
+ // }
|
|
|
+ zap.L().Error("read message error", zap.Error(err))
|
|
|
// Close connect
|
|
|
- // _ = c.repos.OnlineRepository.Offline(c.ctx, c.online)
|
|
|
+ _ = c.repos.OnlineRepository.Offline(c.ctx, c.online)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
var message = deserializeMessage(bytes)
|
|
|
- var log = zlog.With(keys.HeaderRequestId, message.RequestId).With(keys.WebsocketClient, c.UserId)
|
|
|
-
|
|
|
switch message.Type {
|
|
|
case MessageTypePingPong:
|
|
|
- log.Debugf("receive ping message from %s", c.UserId)
|
|
|
+ zap.L().Debug("receive ping message",
|
|
|
+ zap.String("user_id", c.UserId),
|
|
|
+ zap.String("request_id", message.RequestId))
|
|
|
_ = c.repos.OnlineRepository.Heartbeat(c.ctx, c.online)
|
|
|
case MessageTypeUpChating, MessageTypeDownChating:
|
|
|
// Chat dialogue messages
|
|
@@ -83,20 +81,22 @@ func (c *Client) reader() {
|
|
|
}
|
|
|
|
|
|
// Receiver ID format determines whether the receiver is an account or a session
|
|
|
- log.Infof("message receiver: %s", chatingContent.Receiver)
|
|
|
users := c.getReceiverUserIds(chatingContent.Receiver)
|
|
|
- log.Infof("message to users: %v", users)
|
|
|
+ zap.L().Info("received message",
|
|
|
+ zap.String("user_id", c.UserId),
|
|
|
+ zap.String("receiver", chatingContent.Receiver),
|
|
|
+ zap.String("request_id", message.RequestId),
|
|
|
+ zap.Strings("users", users))
|
|
|
for _, id := range users {
|
|
|
var messaging = *message
|
|
|
messaging.Receiver = id
|
|
|
messaging.Content = chatingContent
|
|
|
- log.Infof("Send message %s to %s", c.UserId, id)
|
|
|
|
|
|
// Check if the user is online
|
|
|
if c.firebaseToken != "" {
|
|
|
online, err := c.repos.OnlineRepository.Is(c.ctx, id)
|
|
|
if err != nil {
|
|
|
- log.Error(eris.Wrap(err, "unable to find online user"))
|
|
|
+ zap.L().Error("unable to find online user", zap.Error(err))
|
|
|
continue
|
|
|
}
|
|
|
if !online {
|
|
@@ -140,11 +140,6 @@ func (c *Client) writer() {
|
|
|
for {
|
|
|
select {
|
|
|
case message, ok := <-c.Send:
|
|
|
- // var log = c.logger
|
|
|
- if message != nil {
|
|
|
- // log = log.WithField(keys.HeaderRequestId, message.RequestId)
|
|
|
- }
|
|
|
-
|
|
|
c.UnderlyingConn.SetWriteDeadline(time.Now().Add(c.writeWait))
|
|
|
if !ok {
|
|
|
// The hub closed the channel.
|
|
@@ -170,12 +165,10 @@ func (c *Client) writer() {
|
|
|
if eris.Is(err, models.ErrRecordNotFound) {
|
|
|
break
|
|
|
}
|
|
|
- // log.Error(err)
|
|
|
+ zap.L().Error("unable to find message", zap.Error(err))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- log.Infof("write message %s", message)
|
|
|
case <-ticker.C:
|
|
|
// 到时间发送 ping 信号
|
|
|
c.UnderlyingConn.SetWriteDeadline(time.Now().Add(c.writeWait))
|
|
@@ -237,7 +230,7 @@ func (c *Client) getReceiverUserIds(receiver string) []string {
|
|
|
members, err := c.repos.SessionRepository.GetSessionMembersRemoveOneself(
|
|
|
c.ctx, receiver, c.UserId)
|
|
|
if err != nil {
|
|
|
- zlog.Error(eris.Wrap(err, "unable to get session members"))
|
|
|
+ zap.L().Error("unable to get session members", zap.Error(err))
|
|
|
return []string{}
|
|
|
}
|
|
|
|