@@ -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 查看全部在线用户
2223func 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// 查看用户是否在线
4037func 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// 给用户发送消息
5950func 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}
0 commit comments