修复Im消息发送问题

This commit is contained in:
junleea 2024-06-28 18:10:22 +08:00
parent 372b3127b5
commit c2b3658f72
1 changed files with 10 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"log"
@ -19,6 +20,10 @@ var (
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
// 允许所有来源的连接
return true
},
}
)
@ -45,7 +50,7 @@ func generateRandomHexString(length int) (string, error) {
func GetImKey(c *gin.Context) {
id, _ := c.Get("id")
var req proto.ImKeyReq
if err := c.ShouldBindJSON(&req); err == nil {
if err := c.ShouldBind(&req); err == nil {
id1 := int(id.(float64))
var redis_key string
if id1 < req.To_user_id {
@ -53,7 +58,7 @@ func GetImKey(c *gin.Context) {
} else {
redis_key = strconv.Itoa(req.To_user_id) + "_" + strconv.Itoa(id1) + "_imKey"
}
if worker.IsContainKey(redis_key+"_connection") == true {
if worker.IsContainKey(redis_key) == true {
res := worker.GetRedis(redis_key)
var retrievedData map[string]interface{}
err2 := json.Unmarshal([]byte(res), &retrievedData)
@ -78,8 +83,6 @@ func GetImKey(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": retrievedData, "message": "success"})
return
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": retrievedData, "message": "success"})
return
} else {
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return
@ -126,7 +129,7 @@ func SRMessage(c *gin.Context) {
} else {
redis_key = to_user_id + "_" + strconv.Itoa(id1) + "_imKey"
}
if worker.IsContainKey(redis_key) == false {
if worker.IsContainKey(redis_key+"_connection") == false {
c.JSON(http.StatusOK, gin.H{"code": proto.OperationFailed, "message": "failed"})
return
}
@ -135,7 +138,8 @@ func SRMessage(c *gin.Context) {
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
clients[ws] = true
if err != nil {
log.Println(err)
// log.Println(err)
fmt.Println(err)
return
}
defer ws.Close()