tdy_workerman / phpsocket.io
v1.1.12
2020-01-12 10:06 UTC
Requires
- workerman/channel: >=1.0.0
- workerman/workerman: >=3.5.16
README
composer require tdy_workerman/phpsocket.io
新增支持启动多进程
$io = new SocketIO(2021,array("count"=>2)); //指定开启进程数
扩展客户端带参数时链接namespace
phpsocket.io
基于 socket.io 和 Workerman 的 PHP 服务器端替代实现。
注意
仅支持 socket.io v1.3.0 或更高版本。
本项目只是将 socket.io 通过 workerman 进行了翻译。
更多 API 详见 https://socketio.node.org.cn/docs/server-api/
安装
composer require workerman/phpsocket.io
示例
简单聊天
use Workerman\Worker; use PHPSocketIO\SocketIO; // listen port 2021 for socket.io client $io = new SocketIO(2021); $io->on('connection', function($socket)use($io){ $socket->on('chat message', function($msg)use($io){ $io->emit('chat message', $msg); }); }); Worker::runAll();
另一个聊天示例
https://github.com/walkor/phpsocket.io/blob/master/examples/chat/start_io.php
use Workerman\Worker; use PHPSocketIO\SocketIO; // listen port 2020 for socket.io client $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) { $socket->broadcast->emit('typing', array( 'username' => $socket->username )); }); // when the client emits 'stop typing', we broadcast it to others $socket->on('stop typing', function () use($socket) { $socket->broadcast->emit('stop typing', array( 'username' => $socket->username )); }); // when the user disconnects.. perform this $socket->on('disconnect', function () use($socket) { global $usernames, $numUsers; // remove the username from global usernames list if($socket->addedUser) { unset($usernames[$socket->username]); --$numUsers; // echo globally that this client has left $socket->broadcast->emit('user left', array( 'username' => $socket->username, 'numUsers' => $numUsers )); } }); }); Worker::runAll();
启用 SSL 以支持 https
(phpsocket.io>=1.1.1 && workerman>=3.3.7 所需)
<?php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; use PHPSocketIO\SocketIO; // ssl context $context = array( 'ssl' => array( 'local_cert' => '/your/path/of/server.pem', 'local_pk' => '/your/path/of/server.key', 'verify_peer' => false, ) ); $io = new SocketIO(2021, $context); $io->on('connection', function($connection)use($io){ echo "new connection coming\n"; }); Worker::runAll();
手册
在线演示
运行聊天示例
cd examples/chat
启动
php server.php start 用于调试模式
php server.php start -d 用于守护进程模式
停止
php server.php stop
状态
php server.php status
许可证
MIT