wip: 0.2.2
1. rtc init
This commit is contained in:
@ -39,6 +39,7 @@ const (
|
||||
)
|
||||
|
||||
type roomClient struct {
|
||||
sync.Mutex
|
||||
controller *roomController
|
||||
conn *websocket.Conn
|
||||
ClientType RoomClientType `json:"client_type"`
|
||||
@ -48,6 +49,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"`
|
||||
msgChan chan any
|
||||
}
|
||||
|
||||
@ -90,10 +93,26 @@ func (rc *roomClient) start(ctx context.Context) {
|
||||
rc.controller.Unregister(rc)
|
||||
return
|
||||
case websocket.TextMessage:
|
||||
log.Info("RoomClient: received text message, IP = %s, Id = %s, Name = %s, text = %s", rc.IP, rc.Id, rc.Name, string(bs))
|
||||
log.Debug("RoomClient: received text message, IP = %s, Id = %s, Name = %s, text = %s", rc.IP, rc.Id, rc.Name, string(bs))
|
||||
case websocket.BinaryMessage:
|
||||
log.Debug("RoomClient: received bytes message, IP = %s, Id = %s, Name = %s, text = %s", rc.IP, rc.Id, rc.Name, string(bs))
|
||||
// todo
|
||||
log.Info("RoomClient: received bytes message, IP = %s, Id = %s, Name = %s, text = %s", rc.IP, rc.Id, rc.Name, string(bs))
|
||||
//msg := new(model.Message)
|
||||
//if err = json.Unmarshal(bs, msg); err != nil {
|
||||
// log.Error("RoomClient: unmarshal message failed, id = %s, name = %s, err = %s", rc.Id, rc.Name, err.Error())
|
||||
// continue
|
||||
//}
|
||||
//
|
||||
//switch msg.Type {
|
||||
//case model.WSMessageTypeOffer:
|
||||
// rc.Lock()
|
||||
// rc.Offer = msg.Body
|
||||
// rc.Unlock()
|
||||
//case model.WSMessageTypeCandidate:
|
||||
// rc.Lock()
|
||||
// rc.Candidate = msg.Body
|
||||
// rc.Unlock()
|
||||
//}
|
||||
}
|
||||
}
|
||||
}()
|
||||
@ -134,7 +153,7 @@ func (rc *roomController) Start(ctx context.Context) {
|
||||
}()
|
||||
}
|
||||
|
||||
func (rc *roomController) Register(ip, userAgent string) *roomClient {
|
||||
func (rc *roomController) Register(ip, userAgent string, candidate, offer any) *roomClient {
|
||||
nrc := &roomClient{
|
||||
controller: rc,
|
||||
ClientType: ClientTypeDesktop,
|
||||
@ -144,6 +163,8 @@ func (rc *roomController) Register(ip, userAgent string) *roomClient {
|
||||
Name: tool.RandomName(),
|
||||
msgChan: make(chan any, 1),
|
||||
RegisterAt: time.Now(),
|
||||
Candidate: candidate,
|
||||
Offer: offer,
|
||||
}
|
||||
|
||||
ua := useragent.Parse(userAgent)
|
||||
|
@ -11,10 +11,23 @@ import (
|
||||
|
||||
func LocalRegister() nf.HandlerFunc {
|
||||
return func(c *nf.Ctx) error {
|
||||
ip := c.IP(true)
|
||||
ua := c.Get("User-Agent")
|
||||
type Req struct {
|
||||
Candidate any `json:"candidate"`
|
||||
Offer any `json:"offer"`
|
||||
}
|
||||
|
||||
client := controller.RoomController.Register(ip, ua)
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
ip = c.IP(true)
|
||||
ua = c.Get("User-Agent")
|
||||
)
|
||||
|
||||
if err = c.BodyParser(req); err != nil {
|
||||
return c.Status(http.StatusBadRequest).JSON(map[string]interface{}{"msg": err.Error()})
|
||||
}
|
||||
|
||||
client := controller.RoomController.Register(ip, ua, req.Candidate, req.Offer)
|
||||
|
||||
return resp.Resp200(c, client)
|
||||
}
|
||||
|
14
internal/model/ws.go
Normal file
14
internal/model/ws.go
Normal file
@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
type WSMessageType string
|
||||
|
||||
const (
|
||||
WSMessageTypeOffer WSMessageType = "offer"
|
||||
WSMessageTypeCandidate WSMessageType = "candidate"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Type WSMessageType `json:"type"`
|
||||
Time int64 `json:"time"`
|
||||
Body any `json:"body"`
|
||||
}
|
Reference in New Issue
Block a user