tp下载安装workerman:
composer:? composer require workerman/workerman
代码:
在项目创建php文件
class Worker extends Server
{
protected $socket = 'http://0.0.0.0:2345';
public function __construct()
{
parent::__construct();
global $uidConnections;
$uidConnections = array();
$this->onMessage();
// 或者这样调用
$this->worker->onWorkerStart = function ($worker) {
echo "Worker starting...\n";
};
}
/**
* 收到信息
* @param $connection
* @param $data
*/
public function onMessage()
{
$this->worker->onMessage = function ($connection, $data) {
$connection//消息对象;
???????????$data //消息内容
???????????
???????????可添加对消息的处理代码
????????????????$connection->send(json_encode($msg));//发送消息
????????}
????}
运行该文件:
注意:
运行该文件,要以命令行运行,最好将php配置全局;
为了方便运行可以创建一个.bat文件;
输入
CHCP 65001 php think 运行文件名:server pause
|