Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 626ee12

Browse files
committed
feat: 明确区分 appID、roomID、userID
1 parent 71a612b commit 626ee12

31 files changed

+381
-407
lines changed

‎common/error_code.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const (
1111
OK = 200 // Success
1212
NotLoggedIn = 1000 // 未登录
1313
ParameterIllegal = 1001 // 参数不合法
14-
UnauthorizedUserId = 1002 // 非法的用户Id
14+
UnauthorizedUserID = 1002 // 非法的用户Id
1515
Unauthorized = 1003 // 未授权
1616
ServerError = 1004 // 系统错误
1717
NotData = 1005 // 没有数据
@@ -29,7 +29,7 @@ func GetErrorMessage(code uint32, message string) string {
2929
OK: "Success",
3030
NotLoggedIn: "未登录",
3131
ParameterIllegal: "参数不合法",
32-
UnauthorizedUserId: "非法的用户Id",
32+
UnauthorizedUserID: "非法的用户Id",
3333
Unauthorized: "未授权",
3434
NotData: "没有数据",
3535
ServerError: "系统错误",

‎controllers/base_controller.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/http"
1414
)
1515

16+
// BaseController 基础控制器
1617
type BaseController struct {
1718
gin.Context
1819
}

‎controllers/home/home_controller.go‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,21 @@ import (
1111
"fmt"
1212
"github.com/gin-gonic/gin"
1313
"github.com/spf13/viper"
14+
"gowebsocket/helper"
1415
"gowebsocket/servers/websocket"
1516
"net/http"
16-
"strconv"
1717
)
1818

19-
// 聊天页面
19+
// Index 聊天页面
2020
func Index(c *gin.Context) {
21-
22-
appIdStr := c.Query("appId")
23-
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
24-
appId := uint32(appIdUint64)
25-
if !websocket.InAppIds(appId) {
26-
appId = websocket.GetDefaultAppId()
21+
appID := helper.StrToUint32(c.Query("appID"))
22+
if !websocket.InAppIDs(appID) {
23+
appID = websocket.GetDefaultAppID()
2724
}
28-
29-
fmt.Println("http_request 聊天首页", appId)
30-
25+
fmt.Println("http_request 聊天首页", appID)
3126
data := gin.H{
3227
"title": "聊天首页",
33-
"appId": appId,
28+
"appID": appID,
3429
"httpUrl": viper.GetString("app.httpUrl"),
3530
"webSocketUrl": viper.GetString("app.webSocketUrl"),
3631
}

‎controllers/systems/system_controller.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@ import (
1616
"runtime"
1717
)
1818

19-
// 查询系统状态
19+
// Status 查询系统状态
2020
func Status(c *gin.Context) {
21-
2221
isDebug := c.Query("isDebug")
2322
fmt.Println("http_request 查询系统状态", isDebug)
24-
2523
data := make(map[string]interface{})
26-
2724
numGoroutine := runtime.NumGoroutine()
2825
numCPU := runtime.NumCPU()
2926

3027
// goroutine 的数量
3128
data["numGoroutine"] = numGoroutine
3229
data["numCPU"] = numCPU
33-
3430
// ClientManager 信息
3531
data["managerInfo"] = websocket.GetManagerInfo(isDebug)
36-
3732
controllers.Response(c, common.OK, "", data)
3833
}

‎controllers/user/user_controller.go‎

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@ import (
1212
"github.com/gin-gonic/gin"
1313
"gowebsocket/common"
1414
"gowebsocket/controllers"
15+
"gowebsocket/helper"
1516
"gowebsocket/lib/cache"
1617
"gowebsocket/models"
1718
"gowebsocket/servers/websocket"
1819
"strconv"
1920
)
2021

21-
// 查看全部在线用户
22+
// List 查看全部在线用户
2223
func List(c *gin.Context) {
23-
24-
appIdStr := c.Query("appId")
25-
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
26-
appId := uint32(appIdUint64)
27-
28-
fmt.Println("http_request 查看全部在线用户", appId)
29-
24+
appID := helper.StrToUint32(c.Query("appID"))
25+
roomID := helper.StrToUint32(c.Query("roomID"))
26+
fmt.Println("http_request 查看全部在线用户", appID, roomID)
3027
data := make(map[string]interface{})
3128

32-
userList := websocket.UserList(appId)
29+
userList := websocket.UserList(appID, roomID)
3330
data["userList"] = userList
3431
data["userCount"] = len(userList)
3532

@@ -38,85 +35,68 @@ func List(c *gin.Context) {
3835

3936
// 查看用户是否在线
4037
func Online(c *gin.Context) {
41-
42-
userId := c.Query("userId")
43-
appIdStr := c.Query("appId")
44-
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
45-
appId := uint32(appIdUint64)
46-
47-
fmt.Println("http_request 查看用户是否在线", userId, appIdStr)
38+
userID := c.Query("userID")
39+
appID := helper.StrToUint32(c.Query("appID"))
40+
fmt.Println("http_request 查看用户是否在线", userID, appID)
4841

4942
data := make(map[string]interface{})
50-
51-
online := websocket.CheckUserOnline(appId, userId)
52-
data["userId"] = userId
43+
online := websocket.CheckUserOnline(appID, userID)
44+
data["userID"] = userID
5345
data["online"] = online
54-
5546
controllers.Response(c, common.OK, "", data)
5647
}
5748

5849
// 给用户发送消息
5950
func SendMessage(c *gin.Context) {
6051
// 获取参数
61-
appIdStr := c.PostForm("appId")
62-
userId := c.PostForm("userId")
63-
msgId := c.PostForm("msgId")
52+
appIDStr := c.PostForm("appID")
53+
userID := c.PostForm("userID")
54+
msgID := c.PostForm("msgID")
6455
message := c.PostForm("message")
65-
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
66-
appId := uint32(appIdUint64)
56+
appIDUint64, _ := strconv.ParseInt(appIDStr, 10, 32)
57+
appID := uint32(appIDUint64)
6758

68-
fmt.Println("http_request 给用户发送消息", appIdStr, userId, msgId, message)
59+
fmt.Println("http_request 给用户发送消息", appIDStr, userID, msgID, message)
6960

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

7364
data := make(map[string]interface{})
74-
75-
if cache.SeqDuplicates(msgId) {
76-
fmt.Println("给用户发送消息 重复提交:", msgId)
65+
if cache.SeqDuplicates(msgID) {
66+
fmt.Println("给用户发送消息 重复提交:", msgID)
7767
controllers.Response(c, common.OK, "", data)
78-
7968
return
8069
}
8170

82-
sendResults, err := websocket.SendUserMessage(appId, userId, msgId, message)
71+
sendResults, err := websocket.SendUserMessage(appID, userID, msgID, message)
8372
if err != nil {
8473
data["sendResultsErr"] = err.Error()
8574
}
86-
8775
data["sendResults"] = sendResults
88-
8976
controllers.Response(c, common.OK, "", data)
9077
}
9178

92-
// 给全员发送消息
93-
func SendMessageAll(c *gin.Context) {
79+
// SendRoomMessage 群成员发送消息
80+
func SendRoomMessage(c *gin.Context) {
9481
// 获取参数
95-
appIdStr := c.PostForm("appId")
96-
userId := c.PostForm("userId")
97-
msgId := c.PostForm("msgId")
82+
appID := helper.StrToUint32(c.PostForm("appID"))
83+
roomID := helper.StrToUint32(c.PostForm("roomID"))
84+
userID := c.PostForm("userID")
85+
msgID := c.PostForm("msgID")
9886
message := c.PostForm("message")
99-
appIdUint64, _ := strconv.ParseInt(appIdStr, 10, 32)
100-
appId := uint32(appIdUint64)
101-
102-
fmt.Println("http_request 给全体用户发送消息", appIdStr, userId, msgId, message)
10387

88+
fmt.Println("http_request 给全体用户发送消息", appID, roomID, userID, msgID, message)
10489
data := make(map[string]interface{})
105-
if cache.SeqDuplicates(msgId) {
106-
fmt.Println("给用户发送消息 重复提交:", msgId)
90+
if cache.SeqDuplicates(msgID) {
91+
fmt.Println("给用户发送消息 重复提交:", msgID)
10792
controllers.Response(c, common.OK, "", data)
108-
10993
return
11094
}
111-
112-
sendResults, err := websocket.SendUserMessageAll(appId, userId, msgId, models.MessageCmdMsg, message)
95+
sendResults, err := websocket.SendRoomMsg(appID, roomID, userID, msgID, models.MessageCmdMsg, message)
11396
if err != nil {
11497
data["sendResultsErr"] = err.Error()
115-
11698
}
117-
11899
data["sendResults"] = sendResults
119-
120100
controllers.Response(c, common.OK, "", data)
121101

122102
}

‎go.mod‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/gin-contrib/sse v0.1.0 // indirect
88
github.com/gin-gonic/gin v1.4.0
99
github.com/go-redis/redis v0.0.0-20190719092155-6bc7daa5b1e8
10-
github.com/golang/protobuf v1.3.2
10+
github.com/golang/protobuf v1.4.3
1111
github.com/gorilla/websocket v1.4.0
1212
github.com/hashicorp/hcl v1.0.1-0.20190611123218-cf7d376da96d // indirect
1313
github.com/json-iterator/go v1.1.7 // indirect
@@ -20,8 +20,11 @@ require (
2020
github.com/spf13/viper v1.4.1-0.20190728125013-1b33e8258e07
2121
github.com/subosito/gotenv v1.1.1 // indirect
2222
github.com/ugorji/go v1.1.7 // indirect
23-
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
24-
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect
25-
golang.org/x/text v0.3.2 // indirect
26-
google.golang.org/grpc v1.21.0
23+
golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d // indirect
24+
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43 // indirect
25+
golang.org/x/text v0.3.5 // indirect
26+
google.golang.org/genproto v0.0.0-20210219173056-d891e3cb3b5b // indirect
27+
google.golang.org/grpc v1.35.0
28+
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 // indirect
29+
google.golang.org/protobuf v1.25.0
2730
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /