说明:这个例子只能单进程,要支持多进程或者服务器集群的话需要Channel组件完成进程间通讯,开发也非常简单,可以参考Channel组件集群推送。
代码:
<?php
use Workerman\Worker;
require_once '/WorkMan/Workerman/Autoloader.php';
$worker = new Worker('text://0.0.0.0:6666');
$worker->count = 1;
$worker->conn_ids = array();
$worker->onMessage = function($connection,$data)use($worker) {
//设置id和连接的映射
if(!isset($worker->conn_ids[$connection->id])) {
$worker->conn_ids[$connection->id] =$connection;
return$connection->send('login success, your name is user_'.$connection->id);
}
//解析参数
list($id,$message) = explode(':',$data);
//校验参数...
//全局广播or给特定id发送
if($id==='all') {
broadcast($connection->id,$message);
}else{
sendMessageById($connection->id,$id,$message);
}
};
//当有客户端连接断开时
$worker->onClose=function($connection)use($worker) {
if(isset($worker->conn_ids[$connection->id])) {
//连接断开时删除映射
unset($worker->conn_ids[$connection->id]);
}
};
//向所有验证的用户推送数据
function broadcast($from_id,$message) {
global $worker;
if(!empty($worker->conn_ids) && count($worker->conn_ids) >1) {
foreach($worker->conn_idsas$conn) {
$conn->send("user_{$from_id}say '{$message}' to all\n");
}
}else{
$conn=$worker->conn_ids[$from_id];
$conn->send("Hi,user_{$from_id}。No one online\n");
}
}
//针对id推送数据
function sendMessageById($from_id,$to_id,$message) {
global$worker;
if(isset($worker->conn_ids[$to_id])) {
$conn=$worker->conn_ids[$to_id];
$conn->send("user_{$from_id}say '{$message}' to user_{$to_id}\n");
}else{
$conn=$worker->conn_ids[$from_id];
$conn->send("Hi,user_{$from_id}。user_{$to_id}is not online\n");
}
}
Worker::runAll();
效果如下:
一、启动脚本
二、连接
三、断开user_2的连接
网友评论