wip: 0.2.3

1. websocket hook
  2. rtc init ok
This commit is contained in:
loveuer
2025-05-20 18:04:37 +08:00
parent becbc137c5
commit 16e9d663f4
9 changed files with 223 additions and 246 deletions

View File

@ -26,6 +26,7 @@ func Start(ctx context.Context) <-chan struct{} {
{
api := app.Group("/api/ulocal")
api.Post("/register", handler.LocalRegister())
api.Post("/offer", handler.LocalOffer())
api.Get("/clients", handler.LocalClients())
api.Get("/ws", handler.LocalWS())
}

View File

@ -32,12 +32,22 @@ const (
type RoomMessageType string
const (
RoomMessageTypePing RoomMessageType = "ping"
RoomMessageTypeSelf RoomMessageType = "self"
RoomMessageTypeEnter RoomMessageType = "enter"
RoomMessageTypeLeave RoomMessageType = "leave"
)
type RoomOffer struct {
SDP string `json:"sdp"`
Type string `json:"type"`
}
type RoomCandidate struct {
Candidate string `json:"candidate"`
SdpMid string `json:"sdpMid"`
SdpMLineIndex int `json:"sdpMLineIndex"`
UsernameFragment string `json:"usernameFragment"`
}
type roomClient struct {
sync.Mutex
controller *roomController
@ -49,8 +59,8 @@ type roomClient struct {
Name string `json:"name"`
Id string `json:"id"`
RegisterAt time.Time `json:"register_at"`
Offer any `json:"offer"`
Candidate any `json:"candidate"`
Offer *RoomOffer `json:"offer"`
Candidate *RoomCandidate `json:"candidate"`
msgChan chan any
}
@ -153,7 +163,7 @@ func (rc *roomController) Start(ctx context.Context) {
}()
}
func (rc *roomController) Register(ip, userAgent string, candidate, offer any) *roomClient {
func (rc *roomController) Register(ip, userAgent string, candidate *RoomCandidate, offer *RoomOffer) *roomClient {
nrc := &roomClient{
controller: rc,
ClientType: ClientTypeDesktop,
@ -261,3 +271,18 @@ func (rc *roomController) Unregister(client *roomClient) {
rc.Broadcast(key, map[string]any{"type": RoomMessageTypeLeave, "time": time.Now().UnixMilli(), "body": client})
}
func (rc *roomController) Offer(room, id string, offer *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": "offer",
"offer": offer,
}
}

View File

@ -12,8 +12,8 @@ import (
func LocalRegister() nf.HandlerFunc {
return func(c *nf.Ctx) error {
type Req struct {
Candidate any `json:"candidate"`
Offer any `json:"offer"`
Candidate *controller.RoomCandidate `json:"candidate"`
Offer *controller.RoomOffer `json:"offer"`
}
var (
@ -74,3 +74,26 @@ func LocalWS() nf.HandlerFunc {
return nil
}
}
func LocalOffer() nf.HandlerFunc {
return func(c *nf.Ctx) error {
type Req struct {
Room string `json:"room"`
Id string `json:"id"`
Offer *controller.RoomOffer `json:"offer"`
}
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.Offer(req.Room, req.Id, req.Offer)
return resp.Resp200(c, req.Offer)
}
}