Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft ratchet/ws server #261 #266

Merged
merged 1 commit into from
Feb 29, 2024
Merged

Conversation

d47081
Copy link

@d47081 d47081 commented Feb 28, 2024

my implementation of ws server #261

few notes before merge:

  1. I didn't found how to make allowed hosts filter, so this option not implemented (because my server open for third-party connections) - people can just extend this implementation for their personal needs.

  2. Added few separated config options to the .env.example - even it could be common for WS/WSS

  3. Created ws.php instead of dev.php like in previous version - I think it's more clear for beginners.

  4. it works, but I still getting Error occurred when playing against a computer #256
    maybe forgot to upgrade something..

@d47081
Copy link
Author

d47081 commented Feb 28, 2024

About no.4

image

[2024-02-28T01:53:44.699295+00:00] log.ERROR: Occurred an error {"message":"","file":"/home/chesslablab/chess-server/vendor/chesslablab/php-chess/src/UciEngine/Details/Process.php","line":68,"trace":"#0 /home/chesslablab/chess-server/vendor/chesslablab/php-chess/src/UciEngine/Details/Process.php(85): Chess\\UciEngine\\Details\\Process->readLine()\n#1 /home/chesslablab/chess-server/vendor/chesslablab/php-chess/src/UciEngine/UciEngine.php(32): Chess\\UciEngine\\Details\\Process->readUntil()\n#2 /home/chesslablab/chess-server/src/Game/Game.php(167): Chess\\UciEngine\\UciEngine->__construct()\n#3 /home/chesslablab/chess-server/src/Game/AbstractMode.php(101): ChessServer\\Game\\Game->ai()\n#4 /home/chesslablab/chess-server/src/Command/StockfishCommand.php(66): ChessServer\\Game\\AbstractMode->res()\n#5 /home/chesslablab/chess-server/src/Socket/RatchetWebSocket.php(39): ChessServer\\Command\\StockfishCommand->run()\n#6 /home/chesslablab/chess-server/vendor/cboden/ratchet/src/Ratchet/WebSocket/WsServer.php(75): ChessServer\\Socket\\RatchetWebSocket->onMessage()\n#7 /home/chesslablab/chess-server/vendor/cboden/ratchet/src/Ratchet/WebSocket/WsServer.php(131): Ratchet\\WebSocket\\WsServer->Ratchet\\WebSocket\\{closure}()\n#8 /home/chesslablab/chess-server/vendor/ratchet/rfc6455/src/Messaging/MessageBuffer.php(248): Ratchet\\WebSocket\\WsServer->Ratchet\\WebSocket\\{closure}()\n#9 /home/chesslablab/chess-server/vendor/ratchet/rfc6455/src/Messaging/MessageBuffer.php(194): Ratchet\\RFC6455\\Messaging\\MessageBuffer->processData()\n#10 /home/chesslablab/chess-server/vendor/cboden/ratchet/src/Ratchet/WebSocket/WsServer.php(153): Ratchet\\RFC6455\\Messaging\\MessageBuffer->onData()\n#11 /home/chesslablab/chess-server/vendor/cboden/ratchet/src/Ratchet/Http/HttpServer.php(54): Ratchet\\WebSocket\\WsServer->onMessage()\n#12 /home/chesslablab/chess-server/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(112): Ratchet\\Http\\HttpServer->onMessage()\n#13 /home/chesslablab/chess-server/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(95): Ratchet\\Server\\IoServer->handleData()\n#14 /home/chesslablab/chess-server/vendor/evenement/evenement/src/EventEmitterTrait.php(143): Ratchet\\Server\\IoServer->Ratchet\\Server\\{closure}()\n#15 /home/chesslablab/chess-server/vendor/react/stream/src/Util.php(71): Evenement\\EventEmitter->emit()\n#16 /home/chesslablab/chess-server/vendor/evenement/evenement/src/EventEmitterTrait.php(143): React\\Stream\\Util::React\\Stream\\{closure}()\n#17 /home/chesslablab/chess-server/vendor/react/stream/src/DuplexResourceStream.php(196): Evenement\\EventEmitter->emit()\n#18 /home/chesslablab/chess-server/vendor/react/event-loop/src/StreamSelectLoop.php(246): React\\Stream\\DuplexResourceStream->handleData()\n#19 /home/chesslablab/chess-server/vendor/react/event-loop/src/StreamSelectLoop.php(213): React\\EventLoop\\StreamSelectLoop->waitForStreamActivity()\n#20 /home/chesslablab/chess-server/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(74): React\\EventLoop\\StreamSelectLoop->run()\n#21 /home/chesslablab/chess-server/cli/ratchet/ws.php(37): Ratchet\\Server\\IoServer->run()\n#22 {main}"} []
[2024-02-28T01:53:44.699645+00:00] log.INFO: Sent message {"id":170,"cmd":{"error":"Internal server error"}} []

I have updated php-chess by composer update, and version there is main-dev so maybe that's because I have removed some delays or loop processing, it always happened on ~5 move, like reason in PC thinking a lot..
but I not see any relations with timeout or so, is any ideas?

upd. can't find this file chesslablab/php-chess@c3fd216 is the main-dev changed?

@programarivm
Copy link
Member

programarivm commented Feb 28, 2024

Thanks for sending this PR! Let me have a look and get back to you with an update. These are some answers to the questions:

  1. I didn't found how to make allowed hosts filter, so this option not implemented (because my server open for third-party connections) - people can just extend this implementation for their personal needs.

No problem, the allowed hosts filter should be set up in the prod environment.


  1. Added few separated config options to the .env.example - even it could be common for WS/WSS

Shall we stick with WSS_ or with WS_? Alternatively WEBSOCKET_ can be used to describe both ws and wss protocols.

WEBSOCKET_ADDRESS=0.0.0.0
WEBSOCKET_PORT=8085

I'm of the opinion to use the WEBSOCKET_ prefix and update the environment variables in the ui accordingly.

REACT_APP_WEBSOCKET_SCHEME=wss
REACT_APP_WEBSOCKET_HOST=async.chesslablab.org
REACT_APP_WEBSOCKET_PORT=8443

  1. it works, but I still getting Error occurred when playing against a computer #256
    maybe forgot to upgrade something..

Chess\UciEngine\Stockfish has been deprecated in version 1.4.16 in favor of Chess\UciEngine\UciEngine as described in the docs.

use Chess\UciEngine\UciEngine;
use Chess\UciEngine\Details\Limit;
use Chess\Variant\Classical\Board;

$board = new Board();
$board->play('w', 'e4');

$limit = (new Limit())->setDepth(3);
$stockfish = (new UciEngine('/usr/games/stockfish'))->setOption('Skill Level', 9);
$analysis = $stockfish->analysis($board, $limit);

$board->playLan('b', $analysis['bestmove']);

echo $board->getMovetext();

git pull origin main and composer update will hopefully fix the error occurred when playing against the computer.


Keep it up, and happy learning!

@d47081
Copy link
Author

d47081 commented Feb 28, 2024

Shall we stick with WSS_ or with WS_? Alternatively WEBSOCKET_ can be used to describe both ws and wss protocols.

maybe it's better, I have just created separated options for intuitive understanding, because not all of WSS options available for WS, do at your choice!

git pull origin main and composer update will hopefully fix the error occurred when playing against the computer.

I have tested on my fork before merge it to origin - there is latest version
https://github.com/d47081/chess-server/tree/ratchet-ws

Of course, composer dependencies was updated, but no success

Stockfish installed, and I can interact up to few moves, then server think and alerts with socket error.

I have no ideas what can I do, it looks like version on https://ui.chesslablab.org/ is different than repository.

@d47081
Copy link
Author

d47081 commented Feb 28, 2024

I'm stupid: just forgot to install stockfish

but how does it work for about first ~5 steps, the server not always request stockfish for AI moves?

sorry for disturb, seems that everything works well now.

@d47081
Copy link
Author

d47081 commented Feb 28, 2024

p.s. finally, I think that's better to keep WSS/WS options separately because:

  1. independent way for different servers
  2. more clear for engine beginners (like me)

@d47081
Copy link
Author

d47081 commented Feb 29, 2024

Just found strange moment on attempt to play this morning with PC:

image

I'm starting chess-server with systemd service, in logs only this:

sh: 1: /usr/games/stockfish: not found
PHP Notice:  fwrite(): Write of 8 bytes failed with errno=32 Broken pipe in /home/chesslablab/chess-server/vendor/chesslablab/php-chess/src/UciEngine/Details/Process.php on line 55
PHP Notice:  fwrite(): Write of 4 bytes failed with errno=32 Broken pipe in /home/chesslablab/chess-server/vendor/chesslablab/php-chess/src/UciEngine/Details/Process.php on line 55

restarted service manually, and it work again. it's a second time where I see this connection problem (after update from dev.php), maybe local issue with the server environment, not the script..

@programarivm programarivm merged commit cd9b02a into chesslablab:main Feb 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants