phuongdev89 / yii2-socketio
为Yii2框架提供的简单且强大的socketio
1.2.4
2022-05-20 04:58 UTC
Requires
- ext-json: *
- phuong17889/yii2-base: *
- phuong17889/yii2-cron: *
- predis/predis: ~1.1.0
- symfony/process: ~3.0|~4.0
- yiisoft/yii2: 2.*
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-07 10:29:37 UTC
README
在您的Yii 2项目中使用socket.io的全部功能。
安装
安装nodejs(已测试node 18)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
添加到 composer.json
{ "require" : { "phuongdev89/yii2-socketio": "^2" }, ... "scripts" : { "post-install-cmd": "cd vendor/phuongdev89/yii2-socketio && /usr/bin/npm install", "post-update-cmd": "cd vendor/phuongdev89/yii2-socketio && /usr/bin/npm install" } }
配置
控制台配置(简单分支)
'controllerMap' => [ 'socketio' => [ 'class' => \phuongdev89\socketio\commands\SocketIoCommand::class, 'server' => 'localhost:1367' ], ]
启动sockeio服务器
php yii socketio/start
停止sockeio服务器
php yii socketio/stop
通用配置
'components' =>[ 'broadcastEvent' => [ 'class' => \phuongdev89\socketio\components\BroadcastEvent::class, 'nsp' => 'some_unique_key', //must be changed // Namespaces with events folders 'namespaces' => [ 'app\socketio', ] ], 'broadcastDriver' => [ 'class' => \phuongdev89\socketio\components\BroadcastDriver::class, 'hostname' => 'localhost', 'port' => 6379, ], ]
使用方法
发布者
从服务器到客户端创建发布者
use phuongdev89\socketio\events\EventInterface; use phuongdev89\socketio\events\EventPubInterface; class CountEvent implements EventInterface, EventPubInterface { /** * Channel name. For client side this is nsp. */ public static function broadcastOn(): array { return ['notifications']; } /** * Event name */ public static function name(): string { return 'update_notification_count'; } /** * Emit client event * @param array $data * @return array */ public function fire(array $data): array { return $data; } }
客户端使用socketio接收服务器数据
var socket = io('localhost:1367/notifications'); socket.on('update_notification_count', function(data){ console.log(data) });
用于向客户端广播数据
//Run broadcast to client \phuongdev89\socketio\Broadcast::emit(CountEvent::name(), ['count' => 10]);
接收者
从客户端到服务器创建接收者
use phuongdev89\socketio\events\EventInterface; use phuongdev89\socketio\events\EventSubInterface; class MarkAsReadEvent implements EventInterface, EventSubInterface { /** * Changel name. For client side this is nsp. */ public static function broadcastOn(): array { return ['notifications']; } /** * Event name */ public static function name(): string { return 'mark_as_read_notification'; } /** * Emit client event * @param array $data * @return array */ public function handle(array $data) { // Mark notification as read // And call client update file_put_contents(\Yii::getAlias('@app/file.txt'), json_encode($data)); } }
客户端使用socketio向服务器发送数据
var socket = io('localhost:1367/notifications'); socket.emit('mark_as_read_notification', {id: 10});
带有客户端到服务器检查的接收者
您可以在一个事件中拥有发布者和接收者。如果您需要从客户端到服务器检查数据,您应该使用
- EventPolicyInterface
use phuongdev89\socketio\events\EventSubInterface; use phuongdev89\socketio\events\EventInterface; use phuongdev89\socketio\events\EventPolicyInterface; class MarkAsReadEvent implements EventInterface, EventSubInterface, EventPolicyInterface { /** * Changel name. For client side this is nsp. */ public static function broadcastOn(): array { return ['notifications']; } /** * Event name */ public static function name(): string { return 'mark_as_read_notification'; } /** * @param $data * @return bool */ public function can($data): bool { // Check data from client return true; } /** * Emit client event * @param array $data * @return array */ public function handle(array $data) { // Mark notification as read // And call client update file_put_contents(\Yii::getAlias('@app/file.txt'), json_encode($data)); } }
订阅房间
Socket.io具有房间功能。如果您需要,您应该实现 EventRoomInterface
use phuongdev89\socketio\events\EventPubInterface; use phuongdev89\socketio\events\EventInterface; use phuongdev89\socketio\events\EventRoomInterface; class CountEvent implements EventInterface, EventPubInterface, EventRoomInterface { /** * User id * @var int */ protected $user_id; /** * Channel name. For client side this is nsp. */ public static function broadcastOn(): array { return ['notifications']; } /** * Event name */ public static function name(): string { return 'update_notification_count'; } /** * Socket.io room * @return string */ public function room(): string { return 'user_id_' . $this->user_id; } /** * Emit client event * @param array $data * @return array */ public function fire(array $data): array { $this->user_id = $data['user_id']; return [ 'count' => 10, ]; } }
通过事件订阅房间
您应该使用trait ListenTrait
use phuongdev89\socketio\events\EventPubInterface; use phuongdev89\socketio\events\EventInterface; use phuongdev89\socketio\events\EventRoomInterface; use phuongdev89\socketio\traits\ListenTrait; class CountEvent implements EventInterface, EventPubInterface, EventRoomInterface { use ListenTrait; /** * User id * @var int */ protected $userId; /** * Channel name. For client side this is nsp. */ public static function broadcastOn(): array { return ['notifications']; } /** * Socket.io room * @return string */ public function room(): string { return 'user_id_' . $this->userId; } /** * Emit client event * @param array $data * @return array */ public function fire(array $data): array { $this->userId = $data['userId']; return [ 'count' => 10, ]; } public function handle(array $data) { $this->listen($data); //must place before your code file_put_contents(\Yii::getAlias('@app/../file.txt'), serialize($data)); } public function onLeave($room_id) { // TODO: Implement onLeave() method. } public function onDisconnect($room_id) { // TODO: Implement onDisconnect() method. } public function onJoin($room_id) { // TODO: Implement onJoin() method. } }
客户端使用socketio加入房间并监听数据
var socket = io('localhost:1367/notifications'); socket.emit('join', {room: 'user_id_10'}); // Now you will receive data from 'room-10' socket.on('update_notification_count', function(data){ console.log(data) }); // You can leave room socket.emit('leave');
用于在房间内向客户端广播数据
//Run broadcast to user id = 10 \phuongdev89\socketio\Broadcast::emitToRoom(CountEvent::class, [ 'count' => 4, 'user_id' => 10,//push data to room-10 ]);