feat: reconnect goldgorilla after caller disconnects

pull/1/head
Benyamin Azarkhazin 2023-09-01 16:17:28 +03:30
parent 22599d7dbd
commit eb6ee47e4e
Signed by: benyamin
GPG Key ID: 3AE44F5623C70269
1 changed files with 31 additions and 0 deletions

View File

@ -127,6 +127,9 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i
}) })
peerConn.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { peerConn.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
r.onPeerConnectionStateChange(roomId, id, state) r.onPeerConnectionStateChange(roomId, id, state)
if state == webrtc.PeerConnectionStateClosed && isCaller {
go r.onCallerDisconnected(roomId)
}
}) })
peerConn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { peerConn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
r.onPeerTrack(roomId, id, remote, receiver) r.onPeerTrack(roomId, id, remote, receiver)
@ -148,6 +151,31 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i
return nil return nil
} }
func (r *RoomRepository) onCallerDisconnected(roomId string) {
reqModel := struct {
RoomId string `json:"roomId"`
}{
RoomId: roomId,
}
serializedReqBody, err := json.Marshal(reqModel)
if 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)
}
}
func (r *RoomRepository) onPeerICECandidate(roomId string, id uint64, ic *webrtc.ICECandidate) { func (r *RoomRepository) onPeerICECandidate(roomId string, id uint64, ic *webrtc.ICECandidate) {
if ic == nil { if ic == nil {
return return
@ -187,7 +215,10 @@ func (r *RoomRepository) onPeerConnectionStateChange(roomId string, id uint64, n
room.Lock() room.Lock()
defer room.Unlock() defer room.Unlock()
println("[PC] con_stat", newState.String(), id)
switch newState { switch newState {
case webrtc.PeerConnectionStateDisconnected:
fallthrough
case webrtc.PeerConnectionStateFailed: case webrtc.PeerConnectionStateFailed:
if err := room.Peers[id].Conn.Close(); err != nil { if err := room.Peers[id].Conn.Close(); err != nil {
println(err.Error()) println(err.Error())