You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Alternatively to chess.js which is a great JavaScript library, the chess functionality can also be provided by a WebSocket chess server using a different approach. This is the case for PHP Chess Server as shown in the example below.
// Ws.jsimport{MARKER_TYPE}from"../../../vendor/cm-chessboard/src/extensions/markers/Markers.js";exportdefaultclassWs{constructor(chessboard){this.chessboard=chessboard;this.socket=null;}connect(){console.log('Establishing connection...');returnnewPromise((resolve,reject)=>{this.socket=newWebSocket('wss://async.chesslablab.org:8443');this.socket.onopen=()=>{console.log('Opened connection!');resolve();};this.socket.onmessage=(res)=>{constdata=JSON.parse(res.data);constmsg=Object.keys(data)[0];switch(true){case'error'===msg:
if(data['error']){console.log('Whoops! Something went wrong.');}break;case'/start'===msg:
if(data['/start'].mode==='fen'){if(data['/start'].fen){console.log('Started FEN!');}else{console.log('Invalid FEN, please try again with a different one.');}}break;case'/legal'===msg:
if(data['/legal']){Object.keys(data['/legal'].fen).forEach(key=>{this.chessboard.addMarker(MARKER_TYPE.dot,key);});}break;case'/play_lan'===msg:
if(data['/play_lan'].fen){this.chessboard.setPosition(data['/play_lan'].fen,true);}break;default:
break;}};this.socket.onclose=(err)=>{console.log('The connection has been lost, please reload the page.');reject(err);};this.socket.onerror=(err)=>{console.log('The connection has been lost, please reload the page.');reject(err);};});}send(message){if(this.socket){this.socket.send(message);}}}
I was just wondering if a simple WebSocket example like this one could be added to the cm-chessboard demo.
Thank you, and keep up the great work!
The text was updated successfully, but these errors were encountered:
馃憢 Hi there,
Alternatively to chess.js which is a great JavaScript library, the chess functionality can also be provided by a WebSocket chess server using a different approach. This is the case for PHP Chess Server as shown in the example below.
I was just wondering if a simple WebSocket example like this one could be added to the cm-chessboard demo.
Thank you, and keep up the great work!
The text was updated successfully, but these errors were encountered: