修复Im消息发送问题
This commit is contained in:
parent
372b3127b5
commit
c2b3658f72
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -19,6 +20,10 @@ var (
|
||||||
upgrader = websocket.Upgrader{
|
upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 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) {
|
func GetImKey(c *gin.Context) {
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
var req proto.ImKeyReq
|
var req proto.ImKeyReq
|
||||||
if err := c.ShouldBindJSON(&req); err == nil {
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
id1 := int(id.(float64))
|
id1 := int(id.(float64))
|
||||||
var redis_key string
|
var redis_key string
|
||||||
if id1 < req.To_user_id {
|
if id1 < req.To_user_id {
|
||||||
|
|
@ -53,7 +58,7 @@ func GetImKey(c *gin.Context) {
|
||||||
} else {
|
} else {
|
||||||
redis_key = strconv.Itoa(req.To_user_id) + "_" + strconv.Itoa(id1) + "_imKey"
|
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)
|
res := worker.GetRedis(redis_key)
|
||||||
var retrievedData map[string]interface{}
|
var retrievedData map[string]interface{}
|
||||||
err2 := json.Unmarshal([]byte(res), &retrievedData)
|
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"})
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": retrievedData, "message": "success"})
|
||||||
return
|
return
|
||||||
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": retrievedData, "message": "success"})
|
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
||||||
return
|
return
|
||||||
|
|
@ -126,7 +129,7 @@ func SRMessage(c *gin.Context) {
|
||||||
} else {
|
} else {
|
||||||
redis_key = to_user_id + "_" + strconv.Itoa(id1) + "_imKey"
|
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"})
|
c.JSON(http.StatusOK, gin.H{"code": proto.OperationFailed, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -135,7 +138,8 @@ func SRMessage(c *gin.Context) {
|
||||||
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
clients[ws] = true
|
clients[ws] = true
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
// log.Println(err)
|
||||||
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue