http和php怎么交互,实现http与workerman的交互demo
這里主要用到wm的聊天室demo做了一個示例。
/**
* 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
* @copyright walkor
* @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附件,被人說有廣告嫌疑,已經刪除。不再分享
總結
以上是生活随笔為你收集整理的http和php怎么交互,实现http与workerman的交互demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 懒人畅听如何试音(懒人听书官方网站)
- 下一篇: matlab查找替换指令,使用matla