Last active
August 19, 2016 18:04
-
-
Save leonardorifeli/037db591223698b96379935a2379f6b7 to your computer and use it in GitHub Desktop.
Implemented the Ratchet socket using PHP
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
{ | |
"autoload": { | |
"psr-4": { | |
"Hermes": "src\\Hermes" | |
} | |
}, | |
"require": { | |
"cboden/ratchet": "0.3.*" | |
} | |
} |
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 | |
namespace Hermes\Business\Service; | |
use Ratchet\ConnectionInterface; | |
abstract class ConnectionInformationService | |
{ | |
static public function checkInformations(ConnectionInterface $connection) | |
{ | |
$queryExplode = explode('&', $connection->WebSocket->request->getQuery()); | |
$queryParams = new \stdClass(); | |
foreach ($queryExplode as $queryParam) { | |
$queryParamExplode = explode('=', $queryParam); | |
$queryParamKey = $queryParamExplode[0]; | |
array_shift($queryParamExplode); | |
$queryParamValue = implode($queryParamExplode); | |
$queryParams->$queryParamKey = $queryParamValue; | |
} | |
if (!property_exists($queryParams, 'name')) return false; | |
if (!property_exists($queryParams, 'room')) return false; | |
return $queryParams; | |
} | |
} |
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 | |
namespace Hermes\Entity; | |
class Room | |
{ | |
private $id; | |
private $name; | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function setName($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
} |
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 | |
namespace Hermes\Business\Service; | |
use Hermes\Entity\Room; | |
abstract class RoomService | |
{ | |
static public function getInstance($name) | |
{ | |
$room = new Room(); | |
$room->setName($name); | |
return $room; | |
} | |
} |
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
(function(){ | |
$(document).ready(function() { | |
var name = prompt("Qual seu nome?"); | |
var room = prompt("Qual o nome da sua sala?"); | |
room = room.replace(/\s/g, '').toLowerCase(); | |
var socket = "ws://localhost:777/?&name="+name+"&room="+room+""; | |
var connect = new WebSocket(socket); | |
var users = []; | |
connect.onopen = function(e) | |
{ | |
connect.send(e.message); | |
}; | |
connect.onmessage = function(e) | |
{ | |
connect.send(e.message); | |
}; | |
}); | |
})(); |
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 | |
require 'vendor/autoload.php'; | |
use Ratchet\Server\IoServer; | |
use Ratchet\Http\HttpServer; | |
use Ratchet\WebSocket\WsServer; | |
use Hermes\Business\Service\SocketService; | |
$socket = new SocketService(); | |
$port = 777; | |
$server = IoServer::factory( | |
new HttpServer( | |
new WsServer( | |
$socket | |
) | |
), | |
$port | |
); | |
$loop = $server->loop; | |
$server->run(); |
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 | |
namespace MyApp; | |
use Ratchet\MessageComponentInterface; | |
use Ratchet\ConnectionInterface; | |
class Chat implements MessageComponentInterface { | |
public function onOpen(ConnectionInterface $conn) { | |
} | |
public function onMessage(ConnectionInterface $from, $msg) { | |
} | |
public function onClose(ConnectionInterface $conn) { | |
} | |
public function onError(ConnectionInterface $conn, \Exception $e) { | |
} | |
} |
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 | |
namespace Hermes\Business\Service; | |
use Ratchet\MessageComponentInterface; | |
use Ratchet\ConnectionInterface; | |
use Hermes\Business\Service\ConnectionInformationService; | |
use Hermes\Business\Service\UserService; | |
class SocketService implements MessageComponentInterface | |
{ | |
public static $connections; | |
public function __construct() | |
{ | |
self::$connections = new \SplObjectStorage; | |
} | |
public function onOpen(ConnectionInterface $connection) | |
{ | |
$queryParams = ConnectionInformationService::checkInformations($connection) | |
if(!$queryParams) { | |
$connection->close(); | |
return; | |
} | |
$user = UserService::getNewUser($connection, $queryParams); | |
$connection->session = $user; | |
self::$connections->attach($connection); | |
} | |
public function onMessage(ConnectionInterface $from, $message) | |
{ | |
$usersByRoom = UserService::getUserByRoom($from->session->getRoom(), self::$connections); | |
foreach($usersByRoom as $user) { | |
$user->send("Message of: {$user->session->getName()} - {$message}"); | |
} | |
} | |
public function onClose(ConnectionInterface $connection) | |
{ | |
self::$connections->detach($connection); | |
} | |
public function onError(ConnectionInterface $connection, \Exception $e) | |
{ | |
$connection->close(); | |
} | |
} |
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 | |
namespace Hermes\Entity; | |
class User | |
{ | |
private $id; | |
private $name; | |
private $room; | |
public function setName($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function addRoom($room) | |
{ | |
$room = str_replace(' ', '', $room); | |
$room = strtolower($room); | |
$this->room = $room; | |
} | |
public function getRoom() | |
{ | |
return $this->room; | |
} | |
} |
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 | |
namespace Hermes\Business\Service; | |
use Ratchet\ConnectionInterface; | |
use Hermes\Entity\User; | |
use Hermes\Entity\Room; | |
abstract class UserService | |
{ | |
public function getNewUser(ConnectionInterface $connection, $param) | |
{ | |
$user = new User(); | |
$user->addRoom(RoomService::getInstance($param->room)); | |
$user->setName($param->name); | |
$user->setId($connection->resourceId); | |
return $user; | |
} | |
public function getUserByRoom($room, $connections) | |
{ | |
$users = new \SplObjectStorage; | |
foreach($connections as $connection) { | |
if($connection->session->getRoom != $room) continue; | |
$users->attach($connection); | |
} | |
return $users; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment