使用workerman建立websocket连接

  • 下载workerman源码包
  • 在start.php中引入源码,将源码包vendor目录放在与start.php同级目录

1. start.php 代码如下:

count = 1;
// 连接事件,阻断非法连接
$ws_worker->onConnect = function ($connection) use ($domains)
{/*{{{*/
    // $connection->onWebSocketConnect = function ($connection, $http_header) use ($domains)
    // {
        // // 判断连接来源是否合法,不合法关闭连接
        // if (!in_array(parse_url($_SERVER['HTTP_ORIGIN'])['host'], $domains)) {
            // $connection->close();    
        // }
        // $connection->send("connection success.");
    // };
};/*}}}*/
// 进程启动后建立一个内部通讯端口
$ws_worker->onWorkerStart = function ($ws_worker)
{/*{{{*/
    /**
     * 多个进程监听同一个端口(监听套接字不是继承自父进程)
     * 需要开启端口复用,不然会报Address already in use错误
     */
    // 开启一个内部端口,方便内部系统推送数据,text协议格式,文本+换行符
    /* $inner_text_worker = new Worker("Text://0.0.0.0:10087");
    $inner_text_worker->onMessage = function ($connection, $buffer) use ($ws_worker)
    {
        $data = json_decode($buffer, true);
        $id   = $data['id'];
        // 通过workerman向id的页面推送数据 
        $ret = sendMessageById($id, $buffer);
        // 返回推送结果
        $connection->send($ret ? 'ok' : 'fail');
    };
    $inner_text_worker->listen(); */
    $inner_text_worker = new Worker("http://0.0.0.0:10087");
    $inner_text_worker->onMessage = function ($http) use ($ws_worker)
    {

        $id = $_POST['id'];
        $status = $_POST['status'];
        // 通过workerman向id的页面推送数据 
        $ret = sendMessageById($id, ['id', 'status' => $status]);
        // 返回推送结果
        $http->send($ret ? 'ok' : 'fail');
    };
   $inner_text_worker->listen();
};/*}}}*/
// 增加一个属性,用来保存uid到connection的映射
$ws_worker->uidConnetions = [];
// 客户端发送消息时回调
$ws_worker->onMessage = function ($connection, $data) use ($ws_worker)
{/*{{{*/
    // 判断当前客户端是否已经验证过
    if (!isset($connection->uid)) {
        $connection->uid = $data;

        $ws_worker->uidConnetions[$connection->uid] = $connection;
        return;
    }
};/*}}}*/
// 当客户端连接断开时
$ws_worker->onClose = function ($connection) use ($ws_worker)
{/*{{{*/
    if (isset($connection->uid)) {
        unset($ws_worker->uidConnetions[$connection->uid]);
    }
};/*}}}*/

/**
 * sendMessageById  向指定的客户端推送数据
 * 
 * @param mixed $id 
 * @param mixed $message 
 * @access public
 * @return bool
 */
function sendMessageById($id, $message)
{/*{{{*/
    if (is_array($message))
        $message = json_encode($message);
    global $ws_worker;
    if (isset($ws_worker->uidConnetions[$id])) {
        $connection = $ws_worker->uidConnetions[$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->uidConnetions as $connection) {
        $connection->send($message); 
    } 
}/*}}}*/

Worker::runAll();



2.开启服务进程:php start.php start


3. 发送http请求到对应的端口,代码如下:

4.客户端连接代码:




    
    WebSocket_client
    


   


你可能感兴趣的:(使用workerman建立websocket连接)