// random enough (TODO: function)
: (Date.now().toString(36) + Math.random().toString(36).substr(2, 7)).toUpperCase();
this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant);
- this.conn.onopen = () => {
+ const socketOpenListener = () => {
if (continuation)
{
// TODO: check FEN integrity with opponent
this.conn.send(JSON.stringify({code:"ping", oppid:this.oppId}));
}
};
- this.conn.onmessage = msg => {
+ const socketMessageListener = msg => {
const data = JSON.parse(msg.data);
switch (data.code)
{
break;
}
};
+ const socketCloseListener = () => {
+ console.log("Lost connection -- reconnect"); //TODO: be more subtle than that, reconnect only when needed!
+ this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant);
+ this.conn.addEventListener('open', socketOpenListener);
+ this.conn.addEventListener('message', socketMessageListener);
+ this.conn.addEventListener('close', socketCloseListener);
+ };
+ this.conn.onopen = socketOpenListener;
+ this.conn.onmessage = socketMessageListener;
+ this.conn.onclose = socketCloseListener;
},
methods: {
endGame: function(message) {
},
resign: function() {
if (this.mode == "human" && this.oppConnected)
- this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid}));
+ {
+ try {
+ this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid}));
+ } catch (INVALID_STATE_ERR) {
+ return; //resign failed for some reason...
+ }
+ }
this.endGame("Try again!");
},
updateStorage: function() {
{
// Send game request and wait..
this.clearStorage(); //in case of
- this.conn.send(JSON.stringify({code:"newgame", fen:fen}));
+ try {
+ this.conn.send(JSON.stringify({code:"newgame", fen:fen}));
+ } catch (INVALID_STATE_ERR) {
+ return; //nothing achieved
+ }
document.getElementById("modal-control2").checked = true;
return;
}
try {
this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid}));
} catch(INVALID_STATE_ERR) {
- return; //abort also if we lost connection
+ return; //abort also if sending failed
}
}
new Audio("/sounds/chessmove1.mp3").play();