這里主要用到wm的聊天室demo做了一個(gè)示例。
<?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://m.wtbis.cn/
* @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;
// 自動(dòng)加載類
require_once __DIR__ . '/../../Workerman/Autoloader.php';
Autoloader::setRootPath(__DIR__);
// 這個(gè)55150端口不解析web內(nèi)容,主要用于post和get,
//在web——onMessage事件取出$_GET和$_POST的數(shù)據(jù),相當(dāng)于僅作API接口
$web = new Worker("http://0.0.0.0:55150");
// WebServer進(jìn)程數(shù)量固定為1
$web->count = 1;
//開(kāi)啟一個(gè)AsyncTcpConnection轉(zhuǎn)發(fā)器
// 設(shè)置訪問(wèn)對(duì)方主機(jī)的本地ip及端口(每個(gè)socket連接都會(huì)占用一個(gè)本地端口)
$context_option = array(
'socket' => array(
// ip必須是本機(jī)網(wǎng)卡ip,并且能訪問(wèn)對(duì)方主機(jī),否則無(wú)效
'bindto' => '127.0.0.1:2333',
),
);
$con = new AsyncTcpConnection('ws://127.0.0.1:7272', $context_option);
//用ATC做中轉(zhuǎn)來(lái)和Http的數(shù)據(jù)做收發(fā)交互
//主要WEB啟動(dòng)的時(shí)候做ATC轉(zhuǎn)發(fā)器的事件初始化
$web->onWorkerStart = function($web)
{
file_get_contents('http://a-vi.com/api/send2qw/?WorkerMan_Web回調(diào):服務(wù)啟動(dòng)');
// $web->send('receive success');
// 這個(gè)網(wǎng)址是我的消息接收器,
GLOBAL $con;
// 中轉(zhuǎn)器鏈接到workerman的時(shí)候自動(dòng)以一個(gè)客戶端身份登錄,并保持在線
$con->onConnect = function($con) {
$con->send('{"type":"login","client_name":"鄧偉(企業(yè)微信小秘書(shū))","room_id":"1"}');
};
// 中轉(zhuǎn)器收到workerman消息的時(shí)候做轉(zhuǎn)發(fā)處理
$con->onMessage = function($con, $dat) {
echo "\r\n GC收到來(lái)自GM的消息:$dat \r\n \r\n";
$data=json_decode($dat);
switch($data->type){
// 服務(wù)端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/?電子黑板回發(fā)【登錄】'.$data->client_name);
break;
// 客戶端發(fā)言 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/?電子黑板回發(fā)【消息】('.$data->from_client_name.'說(shuō):)'.$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/?電子黑板回發(fā)【關(guān)閉】'.$data->from_client_name);
break;
}
};
// 開(kāi)始登錄
$con->connect();
};
// web worker 收到來(lái)自http數(shù)據(jù)的時(shí)候取出來(lái)通過(guò)ATC轉(zhuǎn)發(fā)給workerman
$web->onMessage = function($conn, $data)
{
if(isset($data['get']['msg'])){ //注意 ico 請(qǐng)求過(guò)濾
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回執(zhí):'.$data['get']['msg']);
// $web->send('receive success');
print_r($data['get']['msg']);
}
$conn->close("hello\n");
};
// 如果不是在根目錄啟動(dòng),則運(yùn)行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
其他就看附件了吧。
--------------------------------補(bǔ)充,源本發(fā)了完整demo附件,被人說(shuō)有廣告嫌疑,已經(jīng)刪除。不再分享
感謝分享,希望大家多多分享