@@ -99,6 +99,22 @@ func setupRouter() *gin.Engine {
9999 protected := r .Group ("/" )
100100 protected .Use (protectedMiddleware ())
101101 {
102+ /*
103+ * Legacy WebRTC session endpoint
104+ *
105+ * This endpoint is maintained for backward compatibility when users upgrade from a version
106+ * using the legacy HTTP-based signaling method to the new WebSocket-based signaling method.
107+ *
108+ * During the upgrade process, when the "Rebooting device after update..." message appears,
109+ * the browser still runs the previous JavaScript code which polls this endpoint to establish
110+ * a new WebRTC session. Once the session is established, the page will automatically reload
111+ * with the updated code.
112+ *
113+ * Without this endpoint, the stale JavaScript would fail to establish a connection,
114+ * causing users to see the "Rebooting device after update..." message indefinitely
115+ * until they manually refresh the page, leading to a confusing user experience.
116+ */
117+ protected .POST ("/webrtc/session" , handleWebRTCSession )
102118 protected .GET ("/webrtc/signaling/client" , handleLocalWebRTCSignal )
103119 protected .POST ("/cloud/register" , handleCloudRegister )
104120 protected .GET ("/cloud/state" , handleCloudState )
@@ -126,6 +142,37 @@ func setupRouter() *gin.Engine {
126142// TODO: support multiple sessions?
127143var currentSession * Session
128144
145+ func handleWebRTCSession (c * gin.Context ) {
146+ var req WebRTCSessionRequest
147+
148+ if err := c .ShouldBindJSON (& req ); err != nil {
149+ c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
150+ return
151+ }
152+
153+ session , err := newSession (SessionConfig {})
154+ if err != nil {
155+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err })
156+ return
157+ }
158+
159+ sd , err := session .ExchangeOffer (req .Sd )
160+ if err != nil {
161+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err })
162+ return
163+ }
164+ if currentSession != nil {
165+ writeJSONRPCEvent ("otherSessionConnected" , nil , currentSession )
166+ peerConn := currentSession .peerConnection
167+ go func () {
168+ time .Sleep (1 * time .Second )
169+ _ = peerConn .Close ()
170+ }()
171+ }
172+ currentSession = session
173+ c .JSON (http .StatusOK , gin.H {"sd" : sd })
174+ }
175+
129176func handleLocalWebRTCSignal (c * gin.Context ) {
130177 cloudLogger .Infof ("new websocket connection established" )
131178 // Create WebSocket options with InsecureSkipVerify to bypass origin check
0 commit comments