Skip to content

Commit

Permalink
* bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-Ben committed Nov 1, 2023
1 parent 7bfa421 commit 28b4cfe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
29 changes: 20 additions & 9 deletions http/controllers/send_to_client_controller.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package controllers

import (
"encoding/json"
"github.com/goravel/framework/contracts/http"
"github.com/okami-chen/goravel-websocket/servers"
)

type inputData struct {
ClientId string `json:"clientId"`
UserId string `json:"userId"`
SendUserId string `json:"sendUserId"`
Code int `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
ClientId string `json:"clientId" form:"clientId"`
UserId string `json:"userId" form:"userId"`
SendUserId string `json:"sendUserId" form:"sendUserId"`
Code int `json:"code" form:"code"`
Msg string `json:"msg" form:"msg"`
Data string `json:"data" form:"data"`
}

func (r *WebsocketController) SendToClient(ctx http.Context) http.Response {
Expand Down Expand Up @@ -46,7 +47,17 @@ func (r *WebsocketController) SendToClient(ctx http.Context) http.Response {
data.ClientId = ctx.Request().Input("clientId")
data.Code = ctx.Request().InputInt("code")
data.Msg = ctx.Request().Input("msg")
data.Data = ctx.Request().Input("data")

str := ctx.Request().InputMap("data")
bt, e := json.Marshal(str)
if e != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": e.Error(),
"data": []string{},
})
}
msg := string(bt)

if data.UserId != "" {
clients, e := servers.Manager.GetByUserId(data.UserId)
Expand All @@ -60,7 +71,7 @@ func (r *WebsocketController) SendToClient(ctx http.Context) http.Response {

var messageId []string
for _, client := range clients {
id := servers.SendMessage2Client(client.ClientId, data.SendUserId, data.Code, data.Msg, &data.Data)
id := servers.SendMessage2Client(client.ClientId, data.SendUserId, data.Code, data.Msg, &msg)
messageId = append(messageId, id)
}
return ctx.Response().Success().Json(http.Json{
Expand All @@ -72,7 +83,7 @@ func (r *WebsocketController) SendToClient(ctx http.Context) http.Response {
})
}

id := servers.SendMessage2Client(data.ClientId, data.SendUserId, data.Code, data.Msg, &data.Data)
id := servers.SendMessage2Client(data.ClientId, data.SendUserId, data.Code, data.Msg, &msg)

return ctx.Response().Success().Json(http.Json{
"code": 0,
Expand Down
14 changes: 12 additions & 2 deletions http/controllers/send_to_group_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"encoding/json"
"github.com/goravel/framework/contracts/http"
"github.com/okami-chen/goravel-websocket/servers"
)
Expand Down Expand Up @@ -37,9 +38,18 @@ func (r *WebsocketController) SendToGroup(ctx http.Context) http.Response {
groupName := ctx.Request().Input("groupName")
code := ctx.Request().InputInt("code")
msg := ctx.Request().Input("msg")
data := ctx.Request().Input("data")
str := ctx.Request().InputMap("data")
bt, e := json.Marshal(str)
if e != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": e.Error(),
"data": []string{},
})
}
m := string(bt)

messageId := servers.SendMessage2Group(systemId, sendUserId, groupName, code, msg, &data)
messageId := servers.SendMessage2Group(systemId, sendUserId, groupName, code, msg, &m)

return ctx.Response().Success().Json(http.Json{
"code": 0,
Expand Down
14 changes: 12 additions & 2 deletions http/controllers/send_to_system_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"encoding/json"
"github.com/goravel/framework/contracts/http"
"github.com/okami-chen/goravel-websocket/servers"
)
Expand Down Expand Up @@ -35,9 +36,18 @@ func (r *WebsocketController) SendToSystem(ctx http.Context) http.Response {
sendUserId := ctx.Request().Input("sendUserId")
code := ctx.Request().InputInt("code")
msg := ctx.Request().Input("msg")
data := ctx.Request().Input("data")

servers.SendMessage2System(systemId, sendUserId, code, msg, data)
str := ctx.Request().InputMap("data")
bt, e := json.Marshal(str)
if e != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": e.Error(),
"data": []string{},
})
}
m := string(bt)
servers.SendMessage2System(systemId, sendUserId, code, msg, m)

return ctx.Response().Success().Json(http.Json{
"code": 0,
Expand Down
8 changes: 4 additions & 4 deletions servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func GetOnlineList(systemId *string, groupName *string) map[string]interface{} {

// 通过本服务器发送信息
func SendMessage2LocalClient(messageId, clientId string, sendUserId string, code int, msg string, data *string) {
//facades.Log().Debug("发送到通道:" + clientId)
//facades.Log().Debug("发送到通道:" + clientId + ",msg -> " + *data)
ToClientChan <- clientInfo{ClientId: clientId, MessageId: messageId, SendUserId: sendUserId, Code: code, Msg: msg, Data: data}
return
}
Expand Down Expand Up @@ -162,15 +162,15 @@ func WriteMessage() {
if conn, err := Manager.GetByClientId(clientInfo.ClientId); err == nil && conn != nil {
if err := Render(conn.Socket, clientInfo.MessageId, clientInfo.SendUserId, clientInfo.Code, clientInfo.Msg, clientInfo.Data); err != nil {
Manager.DisConnect <- conn
facades.Log().Error("终端设备离线:" + clientInfo.ClientId)
facades.Log().Error("终端设备离线:" + clientInfo.ClientId + ",msg: " + *clientInfo.Data)
//设备失败事件
t := carbon.Now("PRC").ToDateTimeString()
events.NewClientMessageFailEvent(conn.UserId, conn.ClientId, t, clientInfo.MessageId)

//设备离线
events.NewClientOffloneEvent(conn.UserId, conn.ClientId, t)
} else {
facades.Log().Debugf("终端设备消息:%s, 消息编号:%s", clientInfo.ClientId, clientInfo.MessageId)
facades.Log().Debugf("终端设备消息:%s, 消息编号:%s, msg: %s", clientInfo.ClientId, clientInfo.MessageId, *clientInfo.Data)
//设备成功事件
t := carbon.Now("PRC").ToDateTimeString()
events.NewClientMessageSuccessEvent(conn.UserId, conn.ClientId, t, clientInfo.MessageId)
Expand All @@ -192,7 +192,7 @@ func Render(conn *websocket.Conn, messageId string, sendUserId string, code int,
// 启动定时器进行心跳检测
func PingTimer() {
go func() {
interval := facades.Config().Get("websocket.interval", "10")
interval := facades.Config().Get("websocket.interval", "30")
num, err := strconv.Atoi(interval.(string))
if err != nil {
facades.Log().Errorf("类型转换失败: %s ", err.Error())
Expand Down

0 comments on commit 28b4cfe

Please sign in to comment.