实现http与workerman的交互demo

woeoio

这里主要用到wm的聊天室demo做了一个示例。

<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;
use \Workerman\Connection\AsyncTcpConnection;
// 自动加载类
require_once __DIR__ . '/../../Workerman/Autoloader.php';
Autoloader::setRootPath(__DIR__);

// 这个55150端口不解析web内容,主要用于post和get,
//在web——onMessage事件取出$_GET和$_POST的数据,相当于仅作API接口
$web = new Worker("http://0.0.0.0:55150");
// WebServer进程数量固定为1
$web->count = 1;

//开启一个AsyncTcpConnection转发器
// 设置访问对方主机的本地ip及端口(每个socket连接都会占用一个本地端口)
$context_option = array(
    'socket' => array(
    // ip必须是本机网卡ip,并且能访问对方主机,否则无效
    'bindto' => '127.0.0.1:2333',
    ),
);

$con = new AsyncTcpConnection('ws://127.0.0.1:7272', $context_option);
//用ATC做中转来和Http的数据做收发交互
//主要WEB启动的时候做ATC转发器的事件初始化
$web->onWorkerStart = function($web)
{

    file_get_contents('http://a-vi.com/api/send2qw/?WorkerMan_Web回调:服务启动');
    // $web->send('receive success');
    // 这个网址是我的消息接收器,

    GLOBAL $con;
    // 中转器链接到workerman的时候自动以一个客户端身份登录,并保持在线
    $con->onConnect = function($con) {
        $con->send('{"type":"login","client_name":"邓伟(企业微信小秘书)","room_id":"1"}');
    };
    // 中转器收到workerman消息的时候做转发处理
    $con->onMessage = function($con, $dat) {
        echo "\r\n GC收到来自GM的消息:$dat \r\n \r\n";
        $data=json_decode($dat);
        switch($data->type){
        // 服务端ping客户端
        case 'ping':
            $con->send('{"type":"pong"}');
            break;;
        // 登录 更新用户列表
        case 'login':
            //{"type":"login","client_id":xxx,"client_name":"xxx","client_list":"[...]","time":"xxx"}
            file_get_contents('http://a-vi.com/api/send2qw/?电子黑板回发【登录】'.$data->client_name);
        break;
        // 客户端发言 message: {type:say, to_client_id:xx, content:xx}
        case 'say':
            //{"type":"say","act":"talk_text","from_client_id":xxx,"to_client_id":"all/client_id","content":"xxx","time":"xxx"}
            if($data->act=='talk_text'){
                $newmsg=$data->content;
            }else{
                $newmsg='<其他消息>:'.substr($dat,100);
            }
            file_get_contents('http://a-vi.com/api/send2qw/?电子黑板回发【消息】('.$data->from_client_name.'说:)'.$newmsg);
            break;
        // 登录 更新用户列表
        case 'logout':
            //{"type":"logout","from_client_id":xxx,"from_client_name":"xxx","time":"xxx"}
            file_get_contents('http://a-vi.com/api/send2qw/?电子黑板回发【关闭】'.$data->from_client_name);
            break;
        }
    };
    // 开始登录
    $con->connect();
};
// web worker 收到来自http数据的时候取出来通过ATC转发给workerman
$web->onMessage = function($conn, $data)
{
    if(isset($data['get']['msg'])){ //注意 ico 请求过滤
        GLOBAL $con;
        $con->send('{"type":"say","act":"talk_text","to_client_id":"all","to_client_name":"所有人","content":"'.$data['get']['msg'].'"}');
        //ws.send('{"type":"say","act":"talk_text","to_client_id":"all","to_client_name":"所有人","content":"【图灵回答】:'+data.text+'"}');
        // file_get_contents('http://a-vi.com/api/send2qw/?WorkerMan_Web回执:'.$data['get']['msg']);
        // $web->send('receive success');
        print_r($data['get']['msg']);
    }
    $conn->close("hello\n");
};

// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

其他就看附件了吧。

--------------------------------补充,源本发了完整demo附件,被人说有广告嫌疑,已经删除。不再分享

9475 2 1
2个评论

walkor

感谢分享,希望大家多多分享

  • 暂无评论
zjtlongze

怎么删了

  • 暂无评论
年代过于久远,无法发表评论

woeoio

2060
积分
0
获赞数
0
粉丝数
2017-08-15 加入
×
🔝