@@ -10,41 +10,73 @@ let hasSocketEverConnected = false;
10
10
let socket ;
11
11
function connectToWebsocket ( ) {
12
12
if ( socketIsConnecting === true ) {
13
+ console . log ( "aborting connection attempt." ) ;
13
14
return ;
14
15
}
15
16
16
17
socketIsConnecting = true ;
17
18
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
+ }
19
27
20
28
socket . onerror = error => {
21
29
//Is not connected and we haven't yet said that we are done trying to
22
30
//connect, this means that we could never even establish a connection.
23
- if ( socket . readyState != 1 && ! hasSocketEverConnected ) {
31
+ if ( socket . readyState != 1 ) {
24
32
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
+ }
29
41
} else {
30
42
console . log ( "Socket error: " , error )
31
43
}
32
44
} ;
33
45
34
46
socket . onopen = ( ) => {
47
+ closeDialog ( shutdownDialogId ) ;
35
48
closeDialog ( reconnectDialogId ) ;
36
49
37
50
hasSocketEverConnected = true ;
38
51
socketIsConnecting = false ;
39
52
40
53
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.
42
55
socket . onerror = null ;
43
56
44
57
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
+ }
48
80
} ;
49
81
50
82
registerMessageHandler ( socket ) ;
@@ -53,6 +85,7 @@ function connectToWebsocket() {
53
85
} ;
54
86
}
55
87
88
+ const shutdownDialogId = "shutdown-dialog" ;
56
89
const reconnectDialogId = "reconnect-dialog" ;
57
90
function showReconnectDialogIfNotShown ( ) {
58
91
const previousReconnectDialog = document . getElementById ( reconnectDialogId ) ;
@@ -833,6 +866,7 @@ let rounds = 0;
833
866
let roundEndTime = 0 ;
834
867
let gameState = "unstarted" ;
835
868
let drawingTimeSetting = "∞" ;
869
+ let restoreData ;
836
870
837
871
function registerMessageHandler ( targetSocket ) {
838
872
targetSocket . onmessage = event => {
@@ -985,10 +1019,16 @@ function registerMessageHandler(targetSocket) {
985
1019
+ '{{.Translation.Get "custom-words-per-turn-setting"}}: ' + parsed . data . customWordsPerTurn + "%\n"
986
1020
+ '{{.Translation.Get "players-per-ip-limit-setting"}}: ' + parsed . data . clientsPerIpLimit ) ;
987
1021
} 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
+ }
992
1032
}
993
1033
}
994
1034
} ;
@@ -1033,6 +1073,7 @@ function setRoundTimeLeft(timeLeftMs) {
1033
1073
}
1034
1074
1035
1075
function handleReadyEvent ( ready ) {
1076
+ restoreData = null ;
1036
1077
ownerID = ready . ownerId ;
1037
1078
ownID = ready . playerId ;
1038
1079
0 commit comments