//Websocket
use Ratchet\Server\IoServer;use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Wamp\WampServer;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use \Ratchet\Session\SessionProvider;
use React\EventLoop\Factory;
use React\ZMQ\Context;
use React\Socket\Server;
echo "Start to listen...\n";
$pusher = new Pusher;
$loop = Factory::create();
$context = new Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'notification'));
$memcache = new \Memcache();
$memcache->connect('127.0.0.1', 11211);
$memcacheHandler = new Handler\MemcacheSessionHandler($memcache);
$session = new SessionProvider(
new WampServer(
$pusher
),
$memcacheHandler
);
$webSock = new Server($loop);
$webSock->listen(8080, '0.0.0.0');
$server = new IoServer(
new HttpServer(
new WsServer(
$session
)
),
$webSock
);
$loop->run();//class application
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
use Ratchet\MessageComponentInterface;
class Pusher implements WampServerInterface, MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
//echo "Connection {$conn->resourceId} has connected\n";
echo "Connection {$conn->Session->get('userid')} has connected\n";
}
}
//website
$session = new Session();
$session->start();
$session->set('userid',"001");
//output commandline
Start to listen...
connection <blank> has connected