websocket

This commit is contained in:
XM-GO
2023-04-14 17:14:49 +08:00
parent 5852896a32
commit c8418bc969
12 changed files with 296 additions and 5 deletions

51
apps/visual/api/upload.go Normal file
View File

@@ -0,0 +1,51 @@
package api
import (
"github.com/XM-GO/PandaKit/biz"
"github.com/XM-GO/PandaKit/restfulx"
"net/http"
"pandax/apps/visual/entity"
"pandax/apps/visual/services"
"pandax/pkg/tool"
"path"
)
type UploadApi struct {
VisualScreenImageApp services.VisualScreenImageModel
}
func (up *UploadApi) UploadImage(rc *restfulx.ReqCtx) {
_, fileHeader, err := rc.Request.Request.FormFile("imagefile")
biz.ErrIsNil(err, "请传入文件")
local := &tool.Local{Path: "uploads/file"}
link, fileName, err := local.UploadFile(fileHeader)
biz.ErrIsNil(err, "文件上传失败")
up.VisualScreenImageApp.Insert(entity.VisualScreenImage{
FileName: fileName,
FilePath: link,
})
rc.ResData = map[string]string{"fileName": fileName, "filePath": link}
}
func (up *UploadApi) GetImages(rc *restfulx.ReqCtx) {
list := up.VisualScreenImageApp.FindList(entity.VisualScreenImage{})
rc.ResData = list
}
func (up *UploadApi) GetImage(rc *restfulx.ReqCtx) {
actual := path.Join("uploads/file", restfulx.PathParam(rc, "subpath"))
http.ServeFile(
rc.Response.ResponseWriter,
rc.Request.Request,
actual)
}
func (up *UploadApi) DeleteImage(rc *restfulx.ReqCtx) {
fileName := restfulx.QueryParam(rc, "fileName")
biz.NotEmpty(fileName, "请传要删除的图片名")
local := &tool.Local{Path: "uploads/file"}
err := local.DeleteFile(fileName)
biz.ErrIsNil(err, "文件删除失败")
up.VisualScreenImageApp.Delete(fileName)
}

View File

@@ -6,13 +6,16 @@ package api
// 生成人panda
// ==========================================================================
import (
"github.com/XM-GO/PandaKit/biz"
"github.com/XM-GO/PandaKit/model"
"github.com/XM-GO/PandaKit/restfulx"
"github.com/emicklei/go-restful/v3"
"github.com/kakuilan/kgo"
"strings"
"pandax/apps/visual/entity"
"pandax/apps/visual/services"
pxSocket "pandax/pkg/websocket"
)
type VisualScreenApi struct {
@@ -76,3 +79,24 @@ func (p *VisualScreenApi) UpdateScreenStatus(rc *restfulx.ReqCtx) {
restfulx.BindQuery(rc, &screen)
p.VisualScreenApp.Update(screen)
}
func (p *VisualScreenApi) ScreenTwin(request *restful.Request, response *restful.Response) {
screenId := request.QueryParameter("screenId")
biz.IsTrue(screenId != "", "请传组态Id")
newWebsocket, err := pxSocket.NewWebsocket(response.ResponseWriter, request.Request, nil)
biz.ErrIsNil(err, "创建Websocket失败")
if err != nil {
return
}
pxSocket.AddWebSocketByScreenId(screenId, newWebsocket)
go func() {
for {
_, message, err := newWebsocket.Conn.ReadMessage()
if err != nil {
return
}
pxSocket.OnMessage(string(message))
}
}()
}