wip: v0.2.6
This commit is contained in:
@ -25,6 +25,7 @@ func Start(ctx context.Context) <-chan struct{} {
|
||||
|
||||
{
|
||||
api := app.Group("/api/ulocal")
|
||||
api.Post("/register", handler.LocalRegister())
|
||||
api.Post("/offer", handler.LocalOffer())
|
||||
api.Post("/answer", handler.LocalAnswer())
|
||||
api.Get("/clients", handler.LocalClients())
|
||||
|
@ -129,11 +129,13 @@ type roomController struct {
|
||||
sync.Mutex
|
||||
ctx context.Context
|
||||
//rooms map[string]map[string]*roomClient // map[room_id(remote-IP)][Id]
|
||||
pre map[string]*roomClient
|
||||
clients map[string]*roomClient
|
||||
}
|
||||
|
||||
var (
|
||||
RoomController = &roomController{
|
||||
pre: make(map[string]*roomClient),
|
||||
clients: make(map[string]*roomClient),
|
||||
}
|
||||
)
|
||||
@ -142,10 +144,9 @@ func (rc *roomController) Start(ctx context.Context) {
|
||||
rc.ctx = ctx
|
||||
}
|
||||
|
||||
func (rc *roomController) Register(conn *websocket.Conn, ip, userAgent string) *roomClient {
|
||||
func (rc *roomController) Register(ip, userAgent string) *roomClient {
|
||||
nrc := &roomClient{
|
||||
controller: rc,
|
||||
conn: conn,
|
||||
ClientType: ClientTypeDesktop,
|
||||
AppType: RoomAppTypeWeb,
|
||||
IP: ip,
|
||||
@ -163,16 +164,32 @@ func (rc *roomController) Register(conn *websocket.Conn, ip, userAgent string) *
|
||||
nrc.ClientType = ClientTypeTablet
|
||||
}
|
||||
|
||||
log.Debug("controller.room: registry client, IP = %s, Id = %s, Name = %s", nrc.IP, nrc.Id, nrc.Name)
|
||||
rc.Lock()
|
||||
defer rc.Unlock()
|
||||
|
||||
nrc.start(rc.ctx)
|
||||
rc.pre[nrc.Id] = nrc
|
||||
|
||||
nrc.msgChan <- map[string]any{"type": "register", "time": time.Now().UnixMilli(), "body": nrc}
|
||||
rc.Broadcast(map[string]any{"type": "enter", "time": time.Now().UnixMilli(), "body": nrc})
|
||||
return nrc
|
||||
}
|
||||
|
||||
func (rc *roomController) Enter(conn *websocket.Conn, id string) *roomClient {
|
||||
log.Debug("controller.room: registry client, id = %s", id)
|
||||
|
||||
rc.Lock()
|
||||
defer rc.Unlock()
|
||||
|
||||
nrc, ok := rc.pre[id]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
nrc.conn = conn
|
||||
nrc.start(rc.ctx)
|
||||
|
||||
rc.Broadcast(map[string]any{"type": "enter", "time": time.Now().UnixMilli(), "body": nrc})
|
||||
|
||||
delete(rc.pre, nrc.Id)
|
||||
rc.clients[nrc.Id] = nrc
|
||||
rc.Unlock()
|
||||
|
||||
return nrc
|
||||
}
|
||||
|
@ -9,6 +9,19 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func LocalRegister() nf.HandlerFunc {
|
||||
return func(c *nf.Ctx) error {
|
||||
var (
|
||||
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 {
|
||||
list := controller.RoomController.List()
|
||||
@ -27,10 +40,11 @@ func LocalWS() nf.HandlerFunc {
|
||||
}
|
||||
|
||||
return func(c *nf.Ctx) error {
|
||||
var (
|
||||
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 {
|
||||
@ -38,7 +52,7 @@ func LocalWS() nf.HandlerFunc {
|
||||
return err
|
||||
}
|
||||
|
||||
controller.RoomController.Register(conn, ip, ua)
|
||||
controller.RoomController.Enter(conn, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user