Skip to content

Commit

Permalink
新功能: 支持聊天分组
Browse files Browse the repository at this point in the history
  • Loading branch information
link1st committed May 7, 2020
1 parent 72a6af8 commit cacb31f
Show file tree
Hide file tree
Showing 58 changed files with 335 additions and 155 deletions.
14 changes: 14 additions & 0 deletions controllers/home/home_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@
package home

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"gowebsocket/servers/websocket"
"net/http"
"strconv"
)

// 查看用户是否在线
func Index(c *gin.Context) {

appIdStr := c.Query("appId")
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
appId := uint32(appIdUint64)
if !websocket.InAppIds(appId) {
appId = websocket.GetDefaultAppId()
}

fmt.Println("http_request 聊天首页", appId)

data := gin.H{
"title": "聊天首页",
"appId": appId,
"httpUrl": viper.GetString("app.httpUrl"),
"webSocketUrl": viper.GetString("app.webSocketUrl"),
}
Expand Down
23 changes: 13 additions & 10 deletions controllers/user/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import (
func List(c *gin.Context) {

appIdStr := c.Query("appId")
appId, _ := strconv.ParseInt(appIdStr, 10, 32)
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
appId := uint32(appIdUint64)

fmt.Println("http_request 查看全部在线用户", appId)

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

userList := websocket.UserList()
userList := websocket.UserList(appId)
data["userList"] = userList
data["userCount"] = len(userList)

controllers.Response(c, common.OK, "", data)
}
Expand All @@ -39,13 +41,14 @@ func Online(c *gin.Context) {

userId := c.Query("userId")
appIdStr := c.Query("appId")
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
appId := uint32(appIdUint64)

fmt.Println("http_request 查看用户是否在线", userId, appIdStr)
appId, _ := strconv.ParseInt(appIdStr, 10, 32)

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

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

Expand All @@ -59,11 +62,11 @@ func SendMessage(c *gin.Context) {
userId := c.PostForm("userId")
msgId := c.PostForm("msgId")
message := c.PostForm("message")
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
appId := uint32(appIdUint64)

fmt.Println("http_request 给用户发送消息", appIdStr, userId, msgId, message)

appId, _ := strconv.ParseInt(appIdStr, 10, 32)

// TODO::进行用户权限认证,一般是客户端传入TOKEN,然后检验TOKEN是否合法,通过TOKEN解析出来用户ID
// 本项目只是演示,所以直接过去客户端传入的用户ID(userId)

Expand All @@ -76,7 +79,7 @@ func SendMessage(c *gin.Context) {
return
}

sendResults, err := websocket.SendUserMessage(uint32(appId), userId, msgId, message)
sendResults, err := websocket.SendUserMessage(appId, userId, msgId, message)
if err != nil {
data["sendResultsErr"] = err.Error()
}
Expand All @@ -93,11 +96,11 @@ func SendMessageAll(c *gin.Context) {
userId := c.PostForm("userId")
msgId := c.PostForm("msgId")
message := c.PostForm("message")
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
appId := uint32(appIdUint64)

fmt.Println("http_request 给全体用户发送消息", appIdStr, userId, msgId, message)

appId, _ := strconv.ParseInt(appIdStr, 10, 32)

data := make(map[string]interface{})
if cache.SeqDuplicates(msgId) {
fmt.Println("给用户发送消息 重复提交:", msgId)
Expand All @@ -106,7 +109,7 @@ func SendMessageAll(c *gin.Context) {
return
}

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

Expand Down
4 changes: 2 additions & 2 deletions models/msg_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import "gowebsocket/common"
const (
MessageTypeText = "text"

MessageCmdMsg = "msg"
MessageCmdMsg = "msg"
MessageCmdEnter = "enter"
MessageCmdExit = "exit"
MessageCmdExit = "exit"
)

// 消息的定义
Expand Down
62 changes: 35 additions & 27 deletions protobuf/im_protobuf.pb.go

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

1 change: 1 addition & 0 deletions protobuf/im_protobuf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ message SendMsgAllRsp {

// 获取用户列表
message GetUserListReq {
uint32 appId = 1;
}

message GetUserListRsp {
Expand Down
6 changes: 4 additions & 2 deletions servers/grpcclient/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func SendMsgAll(server *models.Server, seq string, appId uint32, userId string,

// 获取用户列表
// link::https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_client/main.go
func GetUserList(server *models.Server) (userIds []string, err error) {
func GetUserList(server *models.Server, appId uint32) (userIds []string, err error) {
userIds = make([]string, 0)

conn, err := grpc.Dial(server.String(), grpc.WithInsecure())
Expand All @@ -77,7 +77,9 @@ func GetUserList(server *models.Server) (userIds []string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

req := protobuf.GetUserListReq{}
req := protobuf.GetUserListReq{
AppId: appId,
}
rsp, err := c.GetUserList(ctx, &req)
if err != nil {
fmt.Println("获取用户列表 发送请求错误:", err)
Expand Down
3 changes: 2 additions & 1 deletion servers/grpcserver/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ func (s *server) GetUserList(c context.Context, req *protobuf.GetUserListReq) (r

fmt.Println("grpc_request 获取本机用户列表", req.String())

appId := req.GetAppId()
rsp = &protobuf.GetUserListRsp{}

// 本机
userList := websocket.GetUserList()
userList := websocket.GetUserList(appId)

setErr(rsp, common.OK, "")
rsp.UserId = userList
Expand Down
34 changes: 23 additions & 11 deletions servers/websocket/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,20 @@ func (manager *ClientManager) GetUserKeys() (userKeys []string) {
}

// 获取用户的key
func (manager *ClientManager) GetUserList() (userList []string) {
func (manager *ClientManager) GetUserList(appId uint32) (userList []string) {

userList = make([]string, 0)

clientManager.UserLock.RLock()
defer clientManager.UserLock.RUnlock()

for _, v := range clientManager.Users {
userList = append(userList, v.UserId)
fmt.Println("GetUserList", v.AppId, v.UserId, v.Addr)
if v.AppId == appId {
userList = append(userList, v.UserId)
}
}

fmt.Println("GetUserList", clientManager.Users)
fmt.Println("GetUserList len:", len(clientManager.Users))

return
}
Expand All @@ -209,11 +210,22 @@ func (manager *ClientManager) GetUserClients() (clients []*Client) {
}

// 向全部成员(除了自己)发送数据
func (manager *ClientManager) sendAll(message []byte, ignore *Client) {
func (manager *ClientManager) sendAll(message []byte, ignoreClient *Client) {

clients := manager.GetUserClients()
for _, conn := range clients {
if conn != ignoreClient {
conn.SendMsg(message)
}
}
}

// 向全部成员(除了自己)发送数据
func (manager *ClientManager) sendAppIdAll(message []byte, appId uint32, ignoreClient *Client) {

clients := manager.GetUserClients()
for _, conn := range clients {
if conn != ignore {
if conn != ignoreClient && conn.AppId == appId {
conn.SendMsg(message)
}
}
Expand Down Expand Up @@ -355,10 +367,10 @@ func ClearTimeoutConnections() {
}

// 获取全部用户
func GetUserList() (userList []string) {
fmt.Println("获取全部用户")
func GetUserList(appId uint32) (userList []string) {
fmt.Println("获取全部用户", appId)

userList = clientManager.GetUserList()
userList = clientManager.GetUserList(appId)

return
}
Expand All @@ -367,6 +379,6 @@ func GetUserList() (userList []string) {
func AllSendMessages(appId uint32, userId string, data string) {
fmt.Println("全员广播", appId, userId, data)

ignore := clientManager.GetUserClient(appId, userId)
clientManager.sendAll([]byte(data), ignore)
ignoreClient := clientManager.GetUserClient(appId, userId)
clientManager.sendAppIdAll([]byte(data), appId, ignoreClient)
}
14 changes: 12 additions & 2 deletions servers/websocket/init_acc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ import (
"time"
)

const (
defaultAppId = 101 // 默认平台Id
)

var (
clientManager = NewClientManager() // 管理者
appIds = []uint32{101, 102} // 全部的平台
clientManager = NewClientManager() // 管理者
appIds = []uint32{defaultAppId, 102, 103, 104} // 全部的平台

serverIp string
serverPort string
Expand Down Expand Up @@ -57,6 +61,12 @@ func InAppIds(appId uint32) (inAppId bool) {
return
}

func GetDefaultAppId() (appId uint32) {
appId = defaultAppId

return
}

// 启动程序
func StartWebSocket() {

Expand Down
6 changes: 3 additions & 3 deletions servers/websocket/user_srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

// 查询所有用户
func UserList() (userList []string) {
func UserList(appId uint32) (userList []string) {

userList = make([]string, 0)
currentTime := uint64(time.Now().Unix())
Expand All @@ -34,9 +34,9 @@ func UserList() (userList []string) {
list []string
)
if IsLocal(server) {
list = GetUserList()
list = GetUserList(appId)
} else {
list, _ = grpcclient.GetUserList(server)
list, _ = grpcclient.GetUserList(server, appId)
}
userList = append(userList, list...)
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/pelletier/go-toml/parser_test.go

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

Loading

0 comments on commit cacb31f

Please sign in to comment.