Run php-cs-fixer fix --rules=@PSR12 websocket.php
This commit is contained in:
parent
08b465753d
commit
411a3db465
@ -1,26 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Ratchet\MessageComponentInterface;
|
use Ratchet\MessageComponentInterface;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
// Make sure composer dependencies have been installed
|
// Make sure composer dependencies have been installed
|
||||||
require __DIR__ . '/vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* chat.php
|
* chat.php
|
||||||
* Send any incoming messages to all connected clients (except sender)
|
* Send any incoming messages to all connected clients (except sender)
|
||||||
*/
|
*/
|
||||||
class MyChat implements MessageComponentInterface {
|
class MyChat implements MessageComponentInterface
|
||||||
|
{
|
||||||
protected $clients;
|
protected $clients;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct()
|
||||||
$this->clients = new \SplObjectStorage;
|
{
|
||||||
|
$this->clients = new \SplObjectStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onOpen(ConnectionInterface $conn) {
|
public function onOpen(ConnectionInterface $conn)
|
||||||
|
{
|
||||||
$this->clients->attach($conn);
|
$this->clients->attach($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onMessage(ConnectionInterface $from, $msg) {
|
public function onMessage(ConnectionInterface $from, $msg)
|
||||||
|
{
|
||||||
foreach ($this->clients as $client) {
|
foreach ($this->clients as $client) {
|
||||||
if ($from != $client) {
|
if ($from != $client) {
|
||||||
$client->send($msg);
|
$client->send($msg);
|
||||||
@ -28,17 +33,19 @@ class MyChat implements MessageComponentInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onClose(ConnectionInterface $conn) {
|
public function onClose(ConnectionInterface $conn)
|
||||||
|
{
|
||||||
$this->clients->detach($conn);
|
$this->clients->detach($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onError(ConnectionInterface $conn, \Exception $e) {
|
public function onError(ConnectionInterface $conn, \Exception $e)
|
||||||
|
{
|
||||||
$conn->close();
|
$conn->close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the server application through the WebSocket protocol on port 8080
|
// Run the server application through the WebSocket protocol on port 8080
|
||||||
$app = new Ratchet\App('localhost', 8080);
|
$app = new Ratchet\App('localhost', 8080);
|
||||||
$app->route('/chat', new MyChat, array('*'));
|
$app->route('/chat', new MyChat(), array('*'));
|
||||||
$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
|
$app->route('/echo', new Ratchet\Server\EchoServer(), array('*'));
|
||||||
$app->run();
|
$app->run();
|
||||||
|
Loading…
Reference in New Issue
Block a user