Skip to content

Commit

Permalink
* 等待热心网友PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-Ben committed Oct 27, 2023
1 parent a4a28f9 commit a08afcf
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 27 deletions.
2 changes: 1 addition & 1 deletion config/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
func init() {
config := facades.Config()
config.Add("websocket", map[string]any{

"interval": config.Env("WEBSOCKET_INTERVAL", 10),
})
}
65 changes: 65 additions & 0 deletions http/controllers/bind_to_group_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package controllers

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

func (r *WebsocketController) BindToGroup(ctx http.Context) http.Response {

validator, err := ctx.Request().Validate(map[string]string{
"clientId": "required",
"userId": "required",
"groupName": "required",
"systemId": "required",
})

if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": err.Error(),
"data": []string{},
})
}

if validator.Fails() {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": validator.Errors().One(),
"data": []string{},
})
}
userId := ctx.Request().Input("userId")
groupName := ctx.Request().Input("groupName")
systemId := ctx.Request().Input("systemId")
if userId != "" {
clients, e := servers.Manager.GetByUserId(userId)
if e != nil {
return ctx.Response().Success().Json(http.Json{
"code": 404,
"msg": "client not found",
"data": []string{},
})
}

for _, client := range clients {
servers.AddClient2Group(systemId, groupName, client.ClientId, client.UserId, "")
}
} else {
client, err := servers.Manager.GetByClientId(ctx.Request().Input("clientId"))
if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 404,
"msg": "client not found",
"data": []string{},
})
}
servers.AddClient2Group(systemId, groupName, client.ClientId, client.UserId, "")
}

return ctx.Response().Success().Json(http.Json{
"code": 0,
"msg": "success",
"data": []string{},
})
}
41 changes: 41 additions & 0 deletions http/controllers/close_client_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package controllers

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

func (r *WebsocketController) CloseClient(ctx http.Context) http.Response {

validator, err := ctx.Request().Validate(map[string]string{
"systemId": "required",
"clientId": "required",
})

if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": err.Error(),
"data": []string{},
})
}

if validator.Fails() {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": validator.Errors().One(),
"data": []string{},
})
}

systemId := ctx.Request().Input("systemId")
clientId := ctx.Request().Input("clientId")

servers.CloseClient(clientId, systemId)

return ctx.Response().Success().Json(http.Json{
"code": 0,
"msg": "success",
"data": []string{clientId},
})
}
55 changes: 55 additions & 0 deletions http/controllers/kick_user_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package controllers

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

func (r *WebsocketController) KickUser(ctx http.Context) http.Response {

validator, err := ctx.Request().Validate(map[string]string{
"userId": "required",
"systemId": "required",
})

if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": err.Error(),
"data": []string{},
})
}

if validator.Fails() {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": validator.Errors().One(),
"data": []string{},
})
}

systemId := ctx.Request().Input("systemId")
userId := ctx.Request().Input("userId")

client, err := servers.Manager.GetByUserId(userId)

if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": err.Error(),
"data": []string{},
})
}

var ids []string
for _, c := range client {
servers.CloseClient(c.ClientId, systemId)
ids = append(ids, c.ClientId)
}

return ctx.Response().Success().Json(http.Json{
"code": 0,
"msg": "success",
"data": ids,
})
}
39 changes: 39 additions & 0 deletions http/controllers/online_list_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package controllers

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

func (r *WebsocketController) OnelineList(ctx http.Context) http.Response {

validator, err := ctx.Request().Validate(map[string]string{
"systemId": "required",
"groupName": "required",
})

if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": err.Error(),
"data": []string{},
})
}

if validator.Fails() {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": validator.Errors().One(),
"data": []string{},
})
}

systemId := ctx.Request().Input("systemId")
groupName := ctx.Request().Input("groupName")

return ctx.Response().Success().Json(http.Json{
"code": 0,
"msg": "success",
"data": servers.GetOnlineList(&systemId, &groupName),
})
}
51 changes: 31 additions & 20 deletions http/controllers/send_to_client_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type inputData struct {
ClientId string `json:"clientId"`
UserId string `json:"userId"`
SendUserId string `json:"sendUserId"`
Code int `json:"code"`
Expand All @@ -16,17 +17,8 @@ type inputData struct {
func (r *WebsocketController) SendToClient(ctx http.Context) http.Response {
//
var data inputData
err := ctx.Request().Bind(&data)
if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": err.Error(),
"data": []string{},
})
}

validator, err := ctx.Request().Validate(map[string]string{
"userId": "required",
"sendUserId": "required",
"code": "required",
"msg": "required",
Expand All @@ -49,25 +41,44 @@ func (r *WebsocketController) SendToClient(ctx http.Context) http.Response {
})
}

clients, err := servers.Manager.GetByUserId(data.UserId)
if err != nil {
data.UserId = ctx.Request().Input("userId")
data.SendUserId = ctx.Request().Input("sendUserId")
data.ClientId = ctx.Request().Input("clientId")
data.Code = ctx.Request().InputInt("code")
data.Msg = ctx.Request().Input("msg")
data.Data = ctx.Request().Input("data")

if data.UserId != "" {
clients, e := servers.Manager.GetByUserId(data.UserId)
if e != nil {
return ctx.Response().Success().Json(http.Json{
"code": 404,
"msg": "client not found",
"data": []string{},
})
}

var messageId []string
for _, client := range clients {
id := servers.SendMessage2Client(client.ClientId, data.SendUserId, data.Code, data.Msg, &data.Data)
messageId = append(messageId, id)
}
return ctx.Response().Success().Json(http.Json{
"code": 404,
"msg": "client not found",
"data": []string{},
"code": 0,
"msg": "success",
"data": http.Json{
"messages": messageId,
},
})
}
var messageId []string
for _, client := range clients {
id := servers.SendMessage2Client(client.ClientId, data.SendUserId, data.Code, data.Msg, &data.Data)
messageId = append(messageId, id)
}

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

return ctx.Response().Success().Json(http.Json{
"code": 0,
"msg": "success",
"data": http.Json{
"messages": messageId,
"messages": []string{id},
},
})
}
49 changes: 49 additions & 0 deletions http/controllers/send_to_group_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controllers

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

func (r *WebsocketController) SendToGroup(ctx http.Context) http.Response {

validator, err := ctx.Request().Validate(map[string]string{
"systemId": "required",
"groupName": "required",
"sendUserId": "required",
"code": "required",
"msg": "required",
"data": "required",
})

if err != nil {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": err.Error(),
"data": []string{},
})
}

if validator.Fails() {
return ctx.Response().Success().Json(http.Json{
"code": 500,
"msg": validator.Errors().One(),
"data": []string{},
})
}

systemId := ctx.Request().Input("systemId")
sendUserId := ctx.Request().Input("sendUserId")
groupName := ctx.Request().Input("groupName")
code := ctx.Request().InputInt("code")
msg := ctx.Request().Input("msg")
data := ctx.Request().Input("data")

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

return ctx.Response().Success().Json(http.Json{
"code": 0,
"msg": "success",
"data": []string{messageId},
})
}
12 changes: 10 additions & 2 deletions servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gorilla/websocket"
"github.com/okami-chen/goravel-websocket/events"
"github.com/okami-chen/goravel-websocket/tools/util"
"strconv"
"time"
)

Expand All @@ -31,7 +32,7 @@ type RetData struct {
}

// 心跳间隔
var heartbeatInterval = 10 * time.Second
var heartbeatInterval = 60 * time.Second

func init() {
ToClientChan = make(chan clientInfo, 1000)
Expand Down Expand Up @@ -191,7 +192,14 @@ func Render(conn *websocket.Conn, messageId string, sendUserId string, code int,
// 启动定时器进行心跳检测
func PingTimer() {
go func() {
ticker := time.NewTicker(heartbeatInterval)
interval := facades.Config().Get("websocket.interval", "10")
num, err := strconv.Atoi(interval.(string))
if err != nil {
facades.Log().Errorf("类型转换失败: %s ", err.Error())
return
}
duration := time.Duration(num) * time.Second
ticker := time.NewTicker(duration)
defer ticker.Stop()
for {
<-ticker.C
Expand Down
11 changes: 7 additions & 4 deletions service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func (receiver *ServiceProvider) Boot(app foundation.Application) {

func (receiver *ServiceProvider) Router() {
r := App.MakeRoute()
r.Prefix("websocket").Get("ws", controllers.NewWebsocketController().Server)
r.Prefix("api/websocket").Get("test", controllers.NewWebsocketController().Test)
r.Prefix("api/websocket").Get("register", controllers.NewWebsocketController().Register)
r.Prefix("api/websocket").Get("send_to_client", controllers.NewWebsocketController().SendToClient)
r.Prefix("api/websocket").Get("ws", controllers.NewWebsocketController().Server)
r.Prefix("api/websocket").Post("register", controllers.NewWebsocketController().Register)
r.Prefix("api/websocket").Post("send_to_client", controllers.NewWebsocketController().SendToClient)
r.Prefix("api/websocket").Post("bind_to_group", controllers.NewWebsocketController().BindToGroup)
r.Prefix("api/websocket").Post("send_to_group", controllers.NewWebsocketController().SendToGroup)
r.Prefix("api/websocket").Post("online_list", controllers.NewWebsocketController().OnelineList)
r.Prefix("api/websocket").Post("kick_user", controllers.NewWebsocketController().KickUser)
go servers.Manager.Start()
go servers.WriteMessage()
servers.PingTimer()
Expand Down

0 comments on commit a08afcf

Please sign in to comment.