~~~
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Timer;
include __DIR__ . '/vendor/autoload.php';
$ws_worker = new Worker("websocket://0.0.0.0:2120");
// 启动4个进程对外提供服务
$ws_worker->count = 1;
$ws_worker->onConnect = function ($connection) {
};
// 进程启动后建立一个内部通讯端口
$ws_worker->onWorkerStart = function ($ws_worker) {
Timer::add(10, function () use ($ws_worker) {
// 遍历当前进程所有的客户端连接,发送当前服务器的时间
foreach ($ws_worker->uidconnections as $connection) {
$connection->send("pong1");
}
});
$inner_text_worker = new Worker("http://0.0.0.0:2121");
$inner_text_worker->onMessage = function (TcpConnection $http,Request $request) use ($ws_worker) {
$_POST=$request->post()?$request->post():$request->get();
$id = intval($_POST['id']);
$message = htmlspecialchars_decode($_POST['content']);
$res = sendMessageById($id, $message);
$http->send($res ? 'ok' : "fails");
};
$inner_text_worker->listen();
};
$ws_worker->uidconnections=array();
// 客户端发送消息时回调
$ws_worker->onMessage = function ($connection, $data) use ($ws_worker) {
if ($data == "ping") {
sendMessageById($connection->uid, "pong");
}
$data = json_decode($data, "true");
// 判断当前客户端是否已经验证过
if (!isset($connection->uid)) {
if ($data["type"] == "from") {
$connection->uid = intval($data["uid"]);
// $ws_worker->connections[$connection->uid] = $connection;
$ws_worker->uidconnections[$connection->uid]= $connection;
}
sendMessageById($connection->uid, ["type" => "message", "data" => "welcome:" . $connection->uid]);
return;
}
};
// 当客户端连接断开时
$ws_worker->onClose = function ($connection) use ($ws_worker) {
if (isset($ws_worker->uidconnections[$connection->uid])) {
unset($ws_worker->uidconnections[$connection->uid]);
}
};
/**
* sendMessageById 向指定的客户端推送数据
*
* @param mixed $id
* @param mixed $message
* @access public
* @return bool
*/
function sendMessageById($id, $message)
{
global $ws_worker;
if (is_array($message))
$message = json_encode($message);
if (isset($ws_worker->uidconnections[intval($id)])) {
$connection = $ws_worker->uidconnections[intval($id)];
$connection->send($message);
return true;
}
return false;
}
/**
* broadcast 向所有用户推送数据
*
* @param mixed $message
* @access public
* @return void
*/
function broadcast($message)
{
global $ws_worker;
foreach ($ws_worker->uidconnections as $connection) {
$connection->send($message);
}
}
if (!defined('GLOBAL_START')) {
Worker::runAll();
}
~~~
- 面试
- php
- laravel
- 安装
- 创建数据
- 创建控制器
- 错误
- 权限认证
- 图形验证码
- 管理员
- 无限分类
- 视频播放
- 推流
- pc页面
- thinkphp5
- 表单提交安全验证
- crontab
- 获取下级所有数据
- 过滤敏感词
- 二维码
- 邀请码
- jwt token
- 笛卡尔
- 高并发解决优化
- 设置多语言
- 计算经纬度两点距离
- 分片上传
- centos swoole
- 查看swoole版本
- centos 安装 redis
- 把整数随机分成n份或者红包
- 生成随机字符串
- 获取上级
- 宝塔搭建nginx推拉流
- 生成二维码海报
- 二维数组去重
- 经纬度 ---边距 和判断是不是范围内
- workerman
- 扩展
- redis
- mysql
- mysql 主从复制
- 定位问题语句
- 列的选择规则
- Enum介绍
- 索引介绍
- 延迟关联
- in 查询陷阱
- limit 优化
- 事务
- 读写分离
- mysql 权限错误
- mysql计算距离
- mysql数据导出导入
- 索引失效
- mysql 语句技巧
- linux
- 配置nginx 高可用
- Nginx内核参数优化
- 配置优化
- nginx-keepalived实现高可用负载
- lvs+keepalived
- centos5关闭防火墙
- nginx操作
- linux操作
- mysql 安装
- php
- 安装phpadmin
- php7安装mongodb
- 查看php位置
- 查找开启的进程端口
- 守护进程
- centos安装php7.2
- centos 错误解决
- 安装php7错误
- mysql 错误
- 支付宝支付
- html
- js 倒计时
- go vscode 配置
- go util
- 获取两个数之间随机数
- java
- lambda
- 随机抽取
- lombok
- springboot
- 常见注解
- 接受参数常用注解
- 文件上传
- 上传错误日志拦截
- 拦截器
- 定时任务
- mybatis
- 关联查询
- 全局配置
- 缓存
- git
- 微信相关
- 体现到零钱
- 服务商转账
- 公众号小程序相关
- 小程序二维码
- svn
- layui