@@ -10,41 +10,73 @@ let hasSocketEverConnected = false;
1010let socket ;
1111function connectToWebsocket ( ) {
1212 if ( socketIsConnecting === true ) {
13+ console . log ( "aborting connection attempt." ) ;
1314 return ;
1415 }
1516
1617 socketIsConnecting = true ;
1718
18- socket = new WebSocket ( `${ rootPath } /v1/lobby/ws` ) ;
19+ try {
20+ socket = new WebSocket ( `${ rootPath } /v1/lobby/ws` ) ;
21+ } catch ( exception ) {
22+ console . log ( "Connection error:" + exception )
23+ socketIsConnecting = false ;
24+ connectToWebsocket ( ) ;
25+ return ;
26+ }
1927
2028 socket . onerror = error => {
2129 //Is not connected and we haven't yet said that we are done trying to
2230 //connect, this means that we could never even establish a connection.
23- if ( socket . readyState != 1 && ! hasSocketEverConnected ) {
31+ if ( socket . readyState != 1 ) {
2432 socketIsConnecting = false ;
25- showTextDialog ( "connection-error-dialog" ,
26- '{{.Translation.Get "error-connecting"}}' ,
27- `{{.Translation.Get "error-connecting-text"}}` ) ;
28- console . log ( "Error establishing connection: " , error ) ;
33+ if ( ! hasSocketEverConnected ) {
34+ showTextDialog ( "connection-error-dialog" ,
35+ '{{.Translation.Get "error-connecting"}}' ,
36+ `{{.Translation.Get "error-connecting-text"}}` ) ;
37+ console . log ( "Error establishing connection: " , error ) ;
38+ } else {
39+ connectToWebsocket ( ) ;
40+ }
2941 } else {
3042 console . log ( "Socket error: " , error )
3143 }
3244 } ;
3345
3446 socket . onopen = ( ) => {
47+ closeDialog ( shutdownDialogId ) ;
3548 closeDialog ( reconnectDialogId ) ;
3649
3750 hasSocketEverConnected = true ;
3851 socketIsConnecting = false ;
3952
4053 socket . onclose = event => {
41- //We want to avoid handling the error multiple times and showing the incorrect dialogs.
54+ //We w to avoid handling the error multiple times and showing the incorrect dialogs.
4255 socket . onerror = null ;
4356
4457 console . log ( "Socket Closed Connection: " , event ) ;
45- console . log ( "Attempting to reestablish socket connection." ) ;
46- showReconnectDialogIfNotShown ( ) ;
47- connectToWebsocket ( ) ;
58+
59+ if ( restoreData && event . reason === "lobby_gone" ) {
60+ console . log ( "Resurrecting lobby ..." , ) ;
61+ fetch ( '/v1/lobby/resurrect' , {
62+ method : 'POST' ,
63+ body : restoreData ,
64+ } ) . then ( ( ) => {
65+ console . log ( "Attempting to reestablish socket connection after resurrection ..." ) ;
66+ socketIsConnecting = false ;
67+ connectToWebsocket ( ) ;
68+ } ) ;
69+
70+ return
71+ }
72+
73+ if ( event . reason !== "lobby_gone" && event . reason !== "server_restart" ) {
74+ console . log ( "Attempting to reestablish socket connection." ) ;
75+ showReconnectDialogIfNotShown ( ) ;
76+ }
77+ if ( event . reason === "server_restart" ) {
78+ connectToWebsocket ( ) ;
79+ }
4880 } ;
4981
5082 registerMessageHandler ( socket ) ;
@@ -53,6 +85,7 @@ function connectToWebsocket() {
5385 } ;
5486}
5587
88+ const shutdownDialogId = "shutdown-dialog" ;
5689const reconnectDialogId = "reconnect-dialog" ;
5790function showReconnectDialogIfNotShown ( ) {
5891 const previousReconnectDialog = document . getElementById ( reconnectDialogId ) ;
@@ -833,6 +866,7 @@ let rounds = 0;
833866let roundEndTime = 0 ;
834867let gameState = "unstarted" ;
835868let drawingTimeSetting = "∞" ;
869+ let restoreData ;
836870
837871function registerMessageHandler ( targetSocket ) {
838872 targetSocket . onmessage = event => {
@@ -985,10 +1019,16 @@ function registerMessageHandler(targetSocket) {
9851019 + '{{.Translation.Get "custom-words-per-turn-setting"}}: ' + parsed . data . customWordsPerTurn + "%\n"
9861020 + '{{.Translation.Get "players-per-ip-limit-setting"}}: ' + parsed . data . clientsPerIpLimit ) ;
9871021 } else if ( parsed . type === "shutdown" ) {
988- socket . onclose = null ;
989- socket . close ( ) ;
990- showDialog ( "shutdown-info" , "Server shutting down" ,
991- document . createTextNode ( "Sorry, but the server is about to shut down. Please come back at a later time." ) ) ;
1022+ console . log ( "Shutdown event received" ) ;
1023+ if ( parsed . data ) {
1024+ restoreData = parsed . data ;
1025+ // FIXMe Text anpassen!
1026+ showDialog ( "shutdown-dialog" , "Server shutting down" ,
1027+ document . createTextNode ( "Sorry, but the server is about to shut down. Attempting to restore lobby on restart ..." ) ) ;
1028+ } else {
1029+ showDialog ( "shutdown-dialog" , "Server shutting down" ,
1030+ document . createTextNode ( "Sorry, but the server is about to shut down. Please come back at a later time." ) ) ;
1031+ }
9921032 }
9931033 }
9941034} ;
@@ -1033,6 +1073,7 @@ function setRoundTimeLeft(timeLeftMs) {
10331073}
10341074
10351075function handleReadyEvent ( ready ) {
1076+ restoreData = null ;
10361077 ownerID = ready . ownerId ;
10371078 ownID = ready . playerId ;
10381079
0 commit comments