PHP code example of tinymeng / worker-socket
1. Go to this page and download the library: Download tinymeng/worker-socket library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tinymeng / worker-socket example snippets
.
├── example 实例代码源文件目录
│ ├── laravel
│ │ ├── EventsController.php 回调事件处理实例
│ │ └── socket.php socket启动文件
│ ├── thinkphp
│ │ ├── Events.php 回调事件处理实例
│ │ └── socket.php socket启动文件
│ └── yii
│ ├── EventsController.php 回调事件处理实例
│ └── socket.php socket启动文件
├── src 代码源文件目录
│ └── Server.php 封装服务基础类
├── composer.json composer文件
├── LICENSE MIT License
└── README.md 说明文件
liunx
[root@izbp153yczpm4pp9pjs0u3z majiameng.com]# php socket.php start
Workerman[server.php] start in DEBUG mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.5.13 PHP version:7.2.6
------------------------ WORKERS -------------------------------
user worker listen processes status
root ChatBusinessWorker none 4 [OK]
root ChatGateway websocket://0.0.0.0:1314 4 [OK]
root Register text://0.0.0.0:1236 1 [OK]
----------------------------------------------------------------
Press Ctrl+C to stop. Start success.
liunx
php socket.php start|stop|status|restart|reload
liunx
php socket.php start -d
/**
* Name: Events.php.
* Author: JiaMeng <[email protected] >
* Description: websocket callback
*/
namespace app\index\controller;
use tinymeng\worker\Server;
use GatewayWorker\Lib\Gateway;
class Events extends Server{
/**
* @var string Socket connect address
*/
protected $socket = 'websocket://0.0.0.0:1314';
/**
* @var string The current class of namespace
*/
protected $eventHandler = 'app\index\controller\Events';
/**
* Description: 当客户端连接时时触发
* @param $client_id
*/
public static function onConnect($client_id){
echo 'client_id : '.$client_id. ', connect ' .PHP_EOL;
}
/**
* Description: 当客户端发来消息时触发
* Author: JiaMeng <[email protected] >
* @param int $client_id 连接id
* @param string $data 具体消息
* @return bool
*/
public static function onMessage($client_id, $data) {
echo 'client : '.$client_id. ',message data :'.$data .PHP_EOL;
}
/**
* Description: 当客户端断开连接时触发
* Author: JiaMeng <[email protected] >
* Updater:
* @param int $client_id 连接id
*/
public static function onClose($client_id) {
echo 'client_id : '.$client_id .' close '.PHP_EOL;
}