Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
link1st committed Aug 4, 2019
1 parent 9d75317 commit 1383d1f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 35 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ net.ipv4.tcp_tw_recycle = 0
- 定时脚本,清理过期未心跳链接 完成
- http接口,获取登录、链接数量 完成
- http接口,发送push、查询有多少人在线 完成
- grpc 程序内部通讯,发送消息
- grpc 程序内部通讯,发送消息 完成
- appIds 一个用户在多个平台登录
- 界面,把所有在线的人拉倒一个群里面,发送消息 完成
- ~~单聊~~、群聊
- 实现分布式,水平扩张
- ~~单聊~~、群聊 完成
- 实现分布式,水平扩张 完成
- 压测脚本
- 文档整理

#### 小项
- 定义文本消息结构 完成
Expand All @@ -218,3 +220,9 @@ net.ipv4.tcp_tw_recycle = 0
- 引入机器人 待定

### 后期可以改进优化的地方
- 登录,使用微信登录 获取昵称、头像等
- 有账号系统、资料系统
- 界面优化、适配手机端
- 消息 文本消息(支持表情)、图片、语音、视频消息
- 微服务注册、发现、熔断等

17 changes: 11 additions & 6 deletions controllers/user/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gowebsocket/common"
"gowebsocket/controllers"
"gowebsocket/lib/cache"
"gowebsocket/servers/users"
"gowebsocket/servers/websocket"
"strconv"
)

Expand All @@ -27,7 +27,7 @@ func List(c *gin.Context) {

data := make(map[string]interface{})

userList := users.UserList()
userList := websocket.UserList()
data["userList"] = userList

controllers.Response(c, common.OK, "", data)
Expand All @@ -44,7 +44,7 @@ func Online(c *gin.Context) {

data := make(map[string]interface{})

online := users.CheckUserOnline(uint32(appId), userId)
online := websocket.CheckUserOnline(uint32(appId), userId)
data["userId"] = userId
data["online"] = online

Expand Down Expand Up @@ -72,10 +72,9 @@ func SendMessage(c *gin.Context) {
return
}

sendResults, err := users.SendUserMessage(uint32(appId), userId, msgId, message)
sendResults, err := websocket.SendUserMessage(uint32(appId), userId, msgId, message)
if err != nil {
data["sendResultsErr"] = err.Error()

}

data["sendResults"] = sendResults
Expand All @@ -96,8 +95,14 @@ func SendMessageAll(c *gin.Context) {
appId, _ := strconv.ParseInt(appIdStr, 10, 32)

data := make(map[string]interface{})
if cache.SeqDuplicates(msgId) {
fmt.Println("给用户发送消息 重复提交:", msgId)
controllers.Response(c, common.OK, "", data)

return
}

sendResults, err := users.SendUserMessageAll(uint32(appId), userId, msgId, message)
sendResults, err := websocket.SendUserMessageAll(uint32(appId), userId, msgId, message)
if err != nil {
data["sendResultsErr"] = err.Error()

Expand Down
5 changes: 2 additions & 3 deletions servers/grpcserver/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"gowebsocket/common"
"gowebsocket/models"
"gowebsocket/protobuf"
"gowebsocket/servers/users"
"gowebsocket/servers/websocket"
"log"
"net"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (s *server) QueryUsersOnline(c context.Context, req *protobuf.QueryUsersOnl

rsp = &protobuf.QueryUsersOnlineRsp{}

online := users.CheckUserOnline(req.GetAppId(), req.GetUserId())
online := websocket.CheckUserOnline(req.GetAppId(), req.GetUserId())

setErr(req, common.OK, "")
rsp.Online = online
Expand All @@ -78,7 +77,7 @@ func (s *server) SendMsg(c context.Context, req *protobuf.SendMsgReq) (rsp *prot
}

data := models.GetTextMsgData(req.GetUserId(), req.GetSeq(), req.GetMsg())
sendResults, err := users.SendUserMessageLocal(req.GetAppId(), req.GetUserId(), data)
sendResults, err := websocket.SendUserMessageLocal(req.GetAppId(), req.GetUserId(), data)
if err != nil {
fmt.Println("系统错误", err)
setErr(rsp, common.ServerError, "")
Expand Down
2 changes: 1 addition & 1 deletion servers/websocket/acc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func LoginController(client *Client, seq string, message []byte) (code uint32, m
}

// 用户登录
login := &Login{
login := &login{
AppId: request.AppId,
UserId: request.UserId,
Client: client,
Expand Down
4 changes: 2 additions & 2 deletions servers/websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const (
)

// 用户登录
type Login struct {
type login struct {
AppId uint32
UserId string
Client *Client
}

// 读取客户端数据
func (l *Login) GetKey() (key string) {
func (l *login) GetKey() (key string) {
key = GetUserKey(l.AppId, l.UserId)

return
Expand Down
23 changes: 13 additions & 10 deletions servers/websocket/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ClientManager struct {
Users map[string]*Client // 登录的用户 // appId+uuid
UserLock sync.RWMutex // 读写锁
Register chan *Client // 连接连接处理
Login chan *Login // 用户登录处理
Login chan *login // 用户登录处理
Unregister chan *Client // 断开连接处理程序
Broadcast chan []byte // 广播 向全部成员发送数据
}
Expand All @@ -33,7 +33,7 @@ func NewClientManager() (clientManager *ClientManager) {
Clients: make(map[*Client]bool),
Users: make(map[string]*Client),
Register: make(chan *Client, 1000),
Login: make(chan *Login, 1000),
Login: make(chan *login, 1000),
Unregister: make(chan *Client, 1000),
Broadcast: make(chan []byte, 1000),
}
Expand Down Expand Up @@ -115,20 +115,22 @@ func (manager *ClientManager) EventRegister(client *Client) {
}

// 用户登录
func (manager *ClientManager) EventLogin(Login *Login) {
func (manager *ClientManager) EventLogin(login *login) {
manager.ClientsLock.RLock()
defer manager.ClientsLock.RUnlock()

client := Login.Client
client := login.Client
// 连接存在,在添加
if _, ok := manager.Clients[Login.Client]; ok {
userKey := Login.GetKey()
manager.AddUsers(userKey, Login.Client)
if _, ok := manager.Clients[login.Client]; ok {
userKey := login.GetKey()
manager.AddUsers(userKey, login.Client)
}

fmt.Println("EventLogin 用户登录", client.Addr, Login.AppId, Login.UserId)
fmt.Println("EventLogin 用户登录", client.Addr, login.AppId, login.UserId)

orderId := helper.GetOrderIdTime()
SendUserMessageAll(login.AppId, login.UserId, orderId, models.GetTextMsgDataEnter(login.UserId, helper.GetOrderIdTime(), "哈喽~"))

AllSendMessages(Login.AppId, Login.UserId, models.GetTextMsgDataEnter(Login.UserId, helper.GetOrderIdTime(), "哈喽~"))
}

// 用户断开连接
Expand All @@ -152,7 +154,8 @@ func (manager *ClientManager) EventUnregister(client *Client) {
fmt.Println("EventUnregister 用户断开连接", client.Addr, client.AppId, client.UserId)

if client.UserId != "" {
AllSendMessages(client.AppId, client.UserId, models.GetTextMsgDataExit(client.UserId, helper.GetOrderIdTime(), "用户已经离开~"))
orderId := helper.GetOrderIdTime()
SendUserMessageAll(client.AppId, client.UserId, orderId, models.GetTextMsgDataExit(client.UserId, helper.GetOrderIdTime(), "用户已经离开~"))
}
}

Expand Down
17 changes: 8 additions & 9 deletions servers/users/user_srv.go → servers/websocket/user_srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Time: 12:27
*/

package users
package websocket

import (
"errors"
Expand All @@ -14,7 +14,6 @@ import (
"gowebsocket/lib/cache"
"gowebsocket/models"
"gowebsocket/servers/grpcclient"
"gowebsocket/servers/websocket"
"time"
)

Expand All @@ -34,8 +33,8 @@ func UserList() (userList []string) {
var (
list []string
)
if websocket.IsLocal(server) {
list = websocket.GetUserList()
if IsLocal(server) {
list = GetUserList()
} else {
list, _ = grpcclient.GetUserList(server)
}
Expand All @@ -49,7 +48,7 @@ func UserList() (userList []string) {
func CheckUserOnline(appId uint32, userId string) (online bool) {
// 全平台查询
if appId == 0 {
for _, appId := range websocket.GetAppIds() {
for _, appId := range GetAppIds() {
online, _ = checkUserOnline(appId, userId)
if online == true {
break
Expand All @@ -64,7 +63,7 @@ func CheckUserOnline(appId uint32, userId string) (online bool) {

// 查询用户 是否在线
func checkUserOnline(appId uint32, userId string) (online bool, err error) {
key := websocket.GetUserKey(appId, userId)
key := GetUserKey(appId, userId)
userOnline, err := cache.GetUserOnlineInfo(key)
if err != nil {
if err == redis.Nil {
Expand Down Expand Up @@ -100,7 +99,7 @@ func SendUserMessage(appId uint32, userId string, msgId, message string) (sendRe
// 给本机用户发送消息
func SendUserMessageLocal(appId uint32, userId string, data string) (sendResults bool, err error) {

client := websocket.GetUserClient(appId, userId)
client := GetUserClient(appId, userId)
if client == nil {
err = errors.New("用户不在线")

Expand All @@ -127,9 +126,9 @@ func SendUserMessageAll(appId uint32, userId string, msgId, message string) (sen
}

for _, server := range servers {
if websocket.IsLocal(server) {
if IsLocal(server) {
data := models.GetTextMsgData(userId, msgId, message)
websocket.AllSendMessages(appId, userId, data)
AllSendMessages(appId, userId, data)
} else {
grpcclient.SendMsgAll(server, msgId, appId, userId, "text", message)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1383d1f

Please sign in to comment.