wip: 0.2.4

还未实现 rtc 握手
This commit is contained in:
loveuer
2025-05-22 17:57:36 +08:00
parent 16e9d663f4
commit 013670b78f
4 changed files with 112 additions and 15 deletions

View File

@ -27,6 +27,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())
api.Get("/ws", handler.LocalWS())
}

View File

@ -230,11 +230,9 @@ func (rc *roomController) Enter(conn *websocket.Conn, id string) {
}
func (rc *roomController) List(room string) []*roomClient {
log.Debug("controller.room: list room = %s", room)
clientList := make([]*roomClient, 0)
rc.Lock()
defer rc.Unlock()
clients, ok := rc.rooms[room]
if !ok {
return clientList
@ -283,6 +281,25 @@ func (rc *roomController) Offer(room, id string, offer *RoomOffer) {
rc.rooms[room][id].msgChan <- map[string]any{
"type": "offer",
"id": id,
"room": room,
"offer": offer,
}
}
func (rc *roomController) Answer(room, id string, answer *RoomOffer) {
if _, ok := rc.rooms[room]; !ok {
return
}
if _, ok := rc.rooms[room][id]; !ok {
return
}
rc.rooms[room][id].msgChan <- map[string]any{
"type": "answer",
"id": id,
"room": room,
"answer": answer,
}
}

View File

@ -97,3 +97,26 @@ func LocalOffer() nf.HandlerFunc {
return resp.Resp200(c, req.Offer)
}
}
func LocalAnswer() nf.HandlerFunc {
return func(c *nf.Ctx) error {
type Req struct {
Room string `json:"room"`
Id string `json:"id"`
Answer *controller.RoomOffer `json:"answer"`
}
var (
err error
req = new(Req)
)
if err = c.BodyParser(req); err != nil {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"err": err.Error()})
}
controller.RoomController.Answer(req.Room, req.Id, req.Answer)
return resp.Resp200(c, req)
}
}