Last active
July 11, 2022 07:11
-
-
Save poulou0/da07cefcb2fe9b0be027911aace6f0e9 to your computer and use it in GitHub Desktop.
Dead simple PHP Websocket with Ratchet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"cboden/ratchet": "^0.4.4", | |
"ext-sockets": "*", | |
"ratchet/pawl": "^0.4.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<script> | |
var conn = new WebSocket('ws://localhost:8080/chat?apiToken=blahblah'); | |
conn.onmessage = function(e) { console.log(e.data); }; | |
conn.onopen = function(e) { conn.send('Hello Me!'); }; | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use function Ratchet\Client\connect; | |
require __DIR__ . '/vendor/autoload.php'; | |
connect('ws://localhost:8080/chat', [], ['token' => 'server'])->then(function($conn) { | |
$conn->on('message', function($msg) use ($conn) { | |
echo "Received: {$msg}\n"; | |
$conn->close(); | |
}); | |
$conn->send('Hello World!'); | |
$conn->close(); | |
}, function ($e) { | |
echo "Could not connect: {$e->getMessage()}\n"; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use GuzzleHttp\Psr7\Request; | |
use Ratchet\MessageComponentInterface; | |
use Ratchet\ConnectionInterface; | |
// Make sure composer dependencies have been installed | |
require __DIR__ . '/vendor/autoload.php'; | |
class MyChat implements MessageComponentInterface { | |
protected $clients; | |
public function __construct() { | |
$this->clients = new \SplObjectStorage; | |
} | |
public function onOpen(ConnectionInterface $conn) { | |
/** @var Request $request */ | |
$request = $conn->httpRequest; | |
// var_dump($request->getUri()->getQuery()); // to read the apiToken | |
$this->clients->attach($conn); | |
} | |
public function onMessage(ConnectionInterface $from, $msg) { | |
/** @var Request $request */ | |
$request = $from->httpRequest; | |
if ($request->getHeaderLine('token')) foreach ($this->clients as $client) { | |
if ($from != $client) { | |
$client->send($msg . random_int(10, 99). ' '.count($this->clients)); | |
} | |
} | |
} | |
public function onClose(ConnectionInterface $conn) { | |
$this->clients->detach($conn); | |
} | |
public function onError(ConnectionInterface $conn, \Exception $e) { | |
$conn->close(); | |
} | |
} | |
// Run the server application through the WebSocket protocol on port 8080 | |
$app = new Ratchet\App('localhost', 8080); | |
$app->route('/chat', new MyChat, array('*')); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment