fix: change the way goldgorilla reconnection to greatape was handled

pull/1/head
Benyamin Azarkhazin 2023-10-16 17:38:11 +03:30
parent e85a01b809
commit 2261bf55f4
Signed by: benyamin
GPG Key ID: 3AE44F5623C70269
4 changed files with 53 additions and 38 deletions

65
app.go
View File

@ -36,6 +36,7 @@ func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, t
panic("[E] can't parse ice.servers.json: " + err.Error())
}
}
startRejoinCH := make(chan bool, 2)
a.conf = &models.ConfigModel{
LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node",
TargetRoom: targetRoom,
@ -43,6 +44,7 @@ func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, t
ICEServers: iceServers,
ICETCPMUXListenPort: iceTCPMUXListenPort,
CustomICEHostCandidateIP: customICEHostCandidateIP,
StartRejoinCH: &startRejoinCH,
}
roomRepo := repositories.NewRoomRepository(a.conf)
a.router = &routers.Router{}
@ -63,23 +65,52 @@ func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, t
func (a *App) Run() {
go func() {
start:
buffer, _ := json.Marshal(map[string]any{"roomId": a.conf.TargetRoom, "svcAddr": a.conf.ServiceAddress})
body := bytes.NewReader(buffer)
c := &http.Client{
Timeout: 8 * time.Second,
}
res, err := c.Post(a.conf.LogjamBaseUrl+"/join", "application/json", body)
if err != nil {
println(err.Error())
time.Sleep(4 * time.Second)
goto start
}
if res.StatusCode > 204 {
resbody, _ := io.ReadAll(res.Body)
println("get /join "+res.Status, string(resbody))
time.Sleep(4 * time.Second)
goto start
*a.conf.StartRejoinCH <- true
for simplyJoin := range *a.conf.StartRejoinCH {
if simplyJoin {
buffer, _ := json.Marshal(map[string]any{"roomId": a.conf.TargetRoom, "svcAddr": a.conf.ServiceAddress})
body := bytes.NewReader(buffer)
c := &http.Client{
Timeout: 8 * time.Second,
}
res, err := c.Post(a.conf.LogjamBaseUrl+"/join", "application/json", body)
if err != nil {
println(err.Error())
time.Sleep(4 * time.Second)
*a.conf.StartRejoinCH <- true
continue
}
if res.StatusCode > 204 {
resbody, _ := io.ReadAll(res.Body)
println("get /join "+res.Status, string(resbody))
time.Sleep(4 * time.Second)
*a.conf.StartRejoinCH <- true
continue
}
} else {
reqModel := struct {
RoomId string `json:"roomId"`
}{
RoomId: a.conf.TargetRoom,
}
serializedReqBody, err := json.Marshal(reqModel)
if err != nil {
println(err.Error())
*a.conf.StartRejoinCH <- true
continue
}
resp, err := http.Post(a.conf.LogjamBaseUrl+"/rejoin", "application/json", bytes.NewReader(serializedReqBody))
if err != nil {
println(err.Error())
*a.conf.StartRejoinCH <- true
continue
}
if resp.StatusCode > 204 {
println("/rejoin", resp.Status)
*a.conf.StartRejoinCH <- true
}
}
}
}()
err := a.router.Serve(a.src)

View File

@ -9,4 +9,5 @@ type ConfigModel struct {
ICETCPMUXListenPort uint `json:"ice_tcpmux_listenPort"`
CustomICEHostCandidateIP string `json:"customICEHostCandidateIP"`
ICEServers []webrtc.ICEServer `json:"iceServers"`
StartRejoinCH *chan bool
}

View File

@ -18,7 +18,7 @@ func (err BigError) Meta() any {
return err.metaData
}
func NewError(msg string, errCode int, meta any) BigError {
func NewError(msg string, errCode int, meta any) error {
if meta == nil {
meta = struct {
}{}
@ -30,7 +30,7 @@ func NewError(msg string, errCode int, meta any) BigError {
}
}
func (BigError) FromErr(err error, errCode int, meta any) BigError {
func (BigError) FromErr(err error, errCode int, meta any) error {
if meta == nil {
meta = err
}

View File

@ -209,28 +209,11 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i
}
func (r *RoomRepository) onCallerDisconnected(roomId string) {
reqModel := struct {
RoomId string `json:"roomId"`
}{
RoomId: roomId,
}
serializedReqBody, err := json.Marshal(reqModel)
if err != nil {
if err := r.ResetRoom(roomId); err != nil {
println(err.Error())
return
}
if err = r.ResetRoom(roomId); err != nil {
println(err.Error())
return
}
resp, err := http.Post(r.conf.LogjamBaseUrl+"/rejoin", "application/json", bytes.NewReader(serializedReqBody))
if err != nil {
println(err.Error())
return
}
if resp.StatusCode > 204 {
println("/rejoin", resp.Status)
}
*r.conf.StartRejoinCH <- false
}
func (r *RoomRepository) onPeerICECandidate(roomId string, id uint64, ic *webrtc.ICECandidate) {