2023-01-10 08:37:57 +00:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gobwas/ws"
|
2023-01-12 03:21:57 +00:00
|
|
|
"github.com/huoxue1/qinglong-go/service/client"
|
2023-01-10 08:37:57 +00:00
|
|
|
"github.com/huoxue1/qinglong-go/utils/res"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Api(group *gin.RouterGroup) {
|
|
|
|
group.GET("/info", info())
|
|
|
|
group.GET("/:id/:name/websocket", wsHandle())
|
|
|
|
}
|
|
|
|
|
|
|
|
func info() gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
2023-01-10 12:18:45 +00:00
|
|
|
ctx.JSON(200, gin.H{"websocket": true, "origins": []string{"*:*"}, "cookie_needed": false, "entropy": int64(3563341155)})
|
2023-01-10 08:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func wsHandle() gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
conn, _, _, err := ws.UpgradeHTTP(ctx.Request, ctx.Writer)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JSON(502, res.Err(502, err))
|
|
|
|
return
|
|
|
|
}
|
2023-01-12 03:21:57 +00:00
|
|
|
|
|
|
|
client.AddWs(ctx.Param("id")+"_"+ctx.Param("name"), conn)
|
2023-01-10 08:37:57 +00:00
|
|
|
}
|
|
|
|
}
|