luoyangwei vor 1 Jahr
Ursprung
Commit
ae1cdd91fd
4 geänderte Dateien mit 20 neuen und 6 gelöschten Zeilen
  1. 1 1
      etc/websocket.test.toml
  2. 1 1
      etc/websocket.toml
  3. 15 4
      server/client.go
  4. 3 0
      websocket.go

+ 1 - 1
etc/websocket.test.toml

@@ -13,7 +13,7 @@ writeWait = 10 # 读超时
 natsUrl = "nats://106.75.230.4:4333" 
 
 [mysql]
-dsn = "root:9RKdJsEQKnjrni9R@tcp(10.23.148.10:3306)/sikey?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
+dsn = "root:9RKdJsEQKnjrni9R@tcp(10.23.148.10:3306)/sikey?charset=utf8mb4&parseTime=true&loc=Local"
 # Ignore ErrRecordNotFound error for logger
 skipDefaultTransaction = true
 # Slow SQL threshold

+ 1 - 1
etc/websocket.toml

@@ -13,7 +13,7 @@ writeWait = 10 # 读超时
 natsUrl = "nats://106.75.230.4:4333" 
 
 [mysql]
-dsn = "root:qq123123@tcp(127.0.0.1:3306)/sikey?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
+dsn = "root:qq123123@tcp(127.0.0.1:3306)/sikey?charset=utf8mb4&parseTime=true&loc=Local"
 # Ignore ErrRecordNotFound error for logger
 skipDefaultTransaction = true
 # Slow SQL threshold

+ 15 - 4
server/client.go

@@ -22,9 +22,9 @@ type Client struct {
 	UnderlyingConn *websocket.Conn
 	online         *models.Online
 
-	isRemotely   bool   // isRemotely 是否是远程连接
-	isSimpleMsg  bool   // isSimpleMsg 是否是简单消息
-	localization string // localization 国际码
+	isSimpleMsg   bool   // isSimpleMsg 是否是简单消息
+	localization  string // localization 国际码
+	firebaseToken string // firebaseToken FCM 推送的 token
 
 	// Send message channel 发送消息
 	// 当用户在线时会通过 Send channel 发送在线消息 但如果用户不在线,
@@ -81,7 +81,18 @@ func (c *Client) reader() {
 				messaging.Content = chatingContent
 				zlog.Info("Send message to ", id)
 
-				c.hub.Message <- &messaging
+				// Check if the user is online
+				if c.firebaseToken != "" {
+					var online bool
+					if online, err = c.repos.OnlineRepository.Is(c.ctx, id); err != nil {
+						zlog.Error(eris.Wrap(err, "unable to find online user"))
+						continue
+					}
+
+					if online {
+						c.hub.Message <- &messaging
+					}
+				}
 			}
 
 			// Reply message id

+ 3 - 0
websocket.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"net/http"
 	"strings"
+	"time"
 
 	"github.com/DeanThompson/ginpprof"
 	"github.com/denisbrodbeck/machineid"
@@ -24,6 +25,8 @@ var configFile = flag.String("f", "./etc/websocket.toml", "the config file")
 var nodeId = flag.Int64("n", 1, "the node id")
 
 func main() {
+	time.Local = time.UTC
+
 	flag.Parse()
 	config.MustLoadConfig(*configFile)
 	gid.SetNodeId(*nodeId)