protected function onBrocast($data) { $chat = $this->socket->getSockets(); $chat->emit($data['type'], $data); $response = new Response(json_encode(array('status' => true))); return $response; }
/** * * * @param [array] $config parameter for server */ public function __construct($config) { $config = new Config($config); $io = new SocketIO($config->get('port')); $io->on('connection', array($this, 'onConnect')); $users = new Collection('\\JustText\\Models\\User'); $rooms = new Collection('\\JustText\\Models\\Room'); $this->service = new Container(new Factory()); $this->service->set('io', $io); $this->service->set('users', $users); $this->service->set('rooms', $rooms); $this->service->set('config', $config); }
use Workerman\Worker; use Workerman\WebServer; use Workerman\Lib\Timer; use PHPSocketIO\SocketIO; // Include autoload.php include __DIR__ . '/vendor/autoload.php'; include __DIR__ . '/vendor/autoload.php'; include __DIR__ . '/config/Config.php'; // Save users' id $uidConnectionMap = array(); // Save the count of online users $lastOnlineCount = 0; // Save the count of online pages $lastOnlinePageCount = 0; // PHPSocketIO $senderIO = new SocketIO(SERVER_SOCKET_PORT); $senderIO->on('connection', function ($socket) { // Login event $socket->on('start_login', function ($uid) use($socket) { global $uidConnectionMap, $lastOnlineCount, $lastOnlinePageCount; if (isset($socket->uid)) { return; } // Refresh user id $uid = (string) $uid; if (!isset($uidConnectionMap[$uid])) { $uidConnectionMap[$uid] = 0; } // Save count of links to each user ++$uidConnectionMap[$uid]; // For uid push
<?php use Workerman\Worker; use Workerman\WebServer; use Workerman\Autoloader; use PHPSocketIO\SocketIO; // composer autoload require_once __DIR__ . '/../../../../../vendor/autoload.php'; $io = new SocketIO(2020); $io->on('connection', function ($socket) { $socket->addedUser = false; // when the client emits 'new message', this listens and executes $socket->on('new message', function ($data) use($socket) { // we tell the client to execute 'new message' $socket->broadcast->emit('new message', array('username' => $socket->username, 'message' => $data)); }); // when the client emits 'add user', this listens and executes $socket->on('add user', function ($username) use($socket) { global $usernames, $numUsers; // we store the username in the socket session for this client $socket->username = $username; // add the client's username to the global list $usernames[$username] = $username; ++$numUsers; $socket->addedUser = true; $socket->emit('login', array('numUsers' => $numUsers)); // echo globally (all clients) that a person has connected $socket->broadcast->emit('user joined', array('username' => $socket->username, 'numUsers' => $numUsers)); }); // when the client emits 'typing', we broadcast it to others $socket->on('typing', function () use($socket) {
use Workerman\Worker; use Workerman\WebServer; use Workerman\Lib\Timer; use PHPSocketIO\SocketIO; // composer 的 autoload 文件 include __DIR__ . '/vendor/autoload.php'; include __DIR__ . '/vendor/workerman/phpsocket.io/src/autoload.php'; // 全局数组保存uid在线数据 $uidConnectionMap = array(); // 记录最后一次广播的在线用户数 $last_online_count = 0; // 记录最后一次广播的在线页面数 $last_online_page_count = 0; // PHPSocketIO服务 $sender_io = new SocketIO(2120); // 客户端发起连接事件时,设置连接socket的各种事件回调 $sender_io->on('connection', function ($socket) { // 当客户端发来登录事件时触发 $socket->on('login', function ($uid) use($socket) { global $uidConnectionMap, $last_online_count, $last_online_page_count; // 已经登录过了 if (isset($socket->uid)) { return; } // 更新对应uid的在线数据 $uid = (string) $uid; if (!isset($uidConnectionMap[$uid])) { $uidConnectionMap[$uid] = 0; } // 这个uid有++$uidConnectionMap[$uid]个socket连接
<?php $loader = (include '../autoload.php.dist'); $loader->add('PHPSocketIO', __DIR__ . '/../src'); use PHPSocketIO\SocketIO; use PHPSocketIO\Connection; use PHPSocketIO\Response\Response; use PHPSocketIO\Event; $socketio = new SocketIO(); $chat = $socketio->getSockets()->on('addme', function (Event\MessageEvent $messageEvent) use(&$chat) { $messageEvent->getConnection()->emit('update', array('msg' => "Welcome {$messageEvent->getMessage()}")); $chat->emit('update', array('msg' => "{$messageEvent->getMessage()} is coming.")); })->on('msg', function (Event\MessageEvent $messageEvent) use(&$chat) { $message = $messageEvent->getMessage(); $chat->emit('update', $message); }); $socketio->listen(8080)->onConnect(function (Connection $connection) use($socketio) { list($host, $port) = $connection->getRemote(); echo "connected {$host}:{$port}\n"; })->onRequest('/', function ($connection, \EventHttpRequest $request) { $response = new Response(file_get_contents(__DIR__ . '/web/index.html')); $response->setContentType('text/html', 'UTF-8'); $connection->sendResponse($response); })->onRequest('/socket.io.js', function ($connection, \EventHttpRequest $request) { $response = new Response(file_get_contents(__DIR__ . '/web/socket.io.js')); $response->setContentType('text/html', 'UTF-8'); $connection->sendResponse($response); })->dispatch();