wip: 0.2.1
1. websocket 连接,退出 优化 2. 基本页面
This commit is contained in:
@ -4,11 +4,36 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/loveuer/nf"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"github.com/loveuer/nf/nft/resp"
|
||||
"github.com/loveuer/ushare/internal/controller"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func LocalRegistry() nf.HandlerFunc {
|
||||
func LocalRegister() nf.HandlerFunc {
|
||||
return func(c *nf.Ctx) error {
|
||||
ip := c.IP(true)
|
||||
ua := c.Get("User-Agent")
|
||||
|
||||
client := controller.RoomController.Register(ip, ua)
|
||||
|
||||
return resp.Resp200(c, client)
|
||||
}
|
||||
}
|
||||
|
||||
func LocalClients() nf.HandlerFunc {
|
||||
return func(c *nf.Ctx) error {
|
||||
room := c.Query("room")
|
||||
if room == "" {
|
||||
return c.Status(http.StatusBadRequest).JSON(map[string]string{"err": "room can't be empty"})
|
||||
}
|
||||
|
||||
list := controller.RoomController.List(room)
|
||||
|
||||
return resp.Resp200(c, list)
|
||||
}
|
||||
}
|
||||
|
||||
func LocalWS() nf.HandlerFunc {
|
||||
upgrader := websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
@ -19,16 +44,19 @@ func LocalRegistry() nf.HandlerFunc {
|
||||
|
||||
return func(c *nf.Ctx) error {
|
||||
|
||||
ip := c.IP(true)
|
||||
ua := c.Get("User-Agent")
|
||||
id := c.Query("id")
|
||||
|
||||
if id == "" {
|
||||
return c.Status(http.StatusBadRequest).JSON(map[string]string{"error": "id is empty"})
|
||||
}
|
||||
|
||||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
log.Error("LocalRegistry: failed to upgrade websocket connection, err = %s", err.Error())
|
||||
log.Error("LocalWS: failed to upgrade websocket connection, err = %s", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
controller.RoomController.Register(conn, ip, ua)
|
||||
controller.RoomController.Enter(conn, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user