feat:完善项目
This commit is contained in:
46
app/api/websocket.go
Normal file
46
app/api/websocket.go
Normal file
@ -0,0 +1,46 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"ArmedPolice/app/handle"
|
||||
"ArmedPolice/app/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Websocket struct{}
|
||||
|
||||
var upGrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
}
|
||||
|
||||
func (*Websocket) Ws(c *gin.Context) {
|
||||
conn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
||||
|
||||
if err != nil {
|
||||
APIFailure(err)(c)
|
||||
return
|
||||
}
|
||||
session := getSession()(c).(*service.Session)
|
||||
|
||||
client := service.NewWebsocket(session.UIDToString(), conn)
|
||||
service.HubMessage.RegisterHandle(client)
|
||||
go client.Write()
|
||||
}
|
||||
|
||||
func (*Websocket) Pong(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "ping"})
|
||||
}
|
||||
|
||||
func (*Websocket) Publish(c *gin.Context) {
|
||||
key := c.Query("key")
|
||||
service.HubMessage.EmitHandle(&service.HubEmit{
|
||||
ID: key,
|
||||
Msg: handle.NewWorkNotice("你有一条待办事项"),
|
||||
})
|
||||
APISuccess()(c)
|
||||
}
|
Reference in New Issue
Block a user