代码拉取完成,页面将自动刷新
同步操作将从 CiLang/gin_chat_example 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/**
@author: cilang
@qq: 1019383856
@bili: https://space.bilibili.com/433915419
@gitee: https://gitee.com/OpencvLZG
@since: 2023/4/21
@desc: //TODO
**/
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"log"
"net/http"
)
//type Hub struct {
// UserId string
// Conn chan []*websocket.Conn
//}
// Hub Manage WsConnects
var Hub = make(map[string]*websocket.Conn)
// HubGroup Manage Group {Key:GroupId,Value:SocketConnects}
var HubGroup = make(map[string][]*websocket.Conn)
// SenderMsg Sender Message
type SenderMsg struct {
UserId string `json:"user_id,required"`
Receptor string `json:"receptor,omitempty"`
RoomId string `json:"room_id,omitempty"`
Msg string `json:"msg,required"`
Type string `json:"type,omitempty"`
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func WsHandle(c *gin.Context) {
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Fatal(err)
}
userId := c.Query("user_id")
Hub[userId] = ws
go ChatHandle(ws)
}
func JoinGroup(c *gin.Context) {
userId := c.Query("user_id")
groupId := c.Query("group_id")
wsConn, ok := Hub[userId]
if !ok {
c.JSON(http.StatusOK, gin.H{"message": "User Is Not Online"})
return
}
HubGroup[groupId] = append(HubGroup[groupId], wsConn)
c.JSON(http.StatusOK, gin.H{"message": "Join Chat Group Successful"})
}
func ChatHandle(ws *websocket.Conn) {
defer func(ws *websocket.Conn) {
ws.CloseHandler()
}(ws)
for {
msg := make([]byte, 512)
senderMsg := new(SenderMsg)
_, msg, err := ws.ReadMessage()
if err != nil {
return
}
err = json.Unmarshal(msg, senderMsg)
if err != nil {
ws.WriteMessage(websocket.TextMessage, []byte("Request Data Format Is Wrong"))
continue
}
//fmt.Printf("%+v\n", senderMsg)
if senderMsg.Type == "point" {
wsConn, ok := Hub[senderMsg.Receptor]
if !ok {
ws.WriteMessage(websocket.TextMessage, []byte("This User Is Not Online"))
continue
}
wsConn.WriteMessage(websocket.TextMessage, msg)
} else if senderMsg.Type == "group" {
wsConns, ok := HubGroup[senderMsg.RoomId]
if !ok {
ws.WriteMessage(websocket.TextMessage, []byte("This Room Is Not Online"))
} else {
for _, wsConn := range wsConns {
wsConn.WriteMessage(websocket.TextMessage, msg)
}
}
} else {
ws.WriteMessage(websocket.TextMessage, []byte("Unknown Msg Type"))
}
}
}
func main() {
r := gin.Default()
r.GET("/ws/pointChat", WsHandle)
r.POST("/chat/join_group", JoinGroup)
r.Run("127.0.0.1:80")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。