目录
1、下载源码
源码地址:https://github.com/imgaoxianshen/swoole_thinkphp.git
下载之后我更改以下两处代码 Application\index\controller\index.php 、server\ws.php 可以复制对应修改一下:
<?php
namespace app\index\controller;
use app\common\lib\Util;
use app\common\lib\ali\Sms;
class Index
{
public function index()
{
if(empty($_GET['game_id'])){
return Util::show(config('code.error'),'error1');
}
if(empty($_GET['content'])){
return Util::show(config('code.error'),'error2');
}
$data = [
'user' => '用户'.rand(0,2000),
'content' =>$_GET['content']
];
foreach($_POST['http_server']->ports[1]->connections as $fd){
$_POST['http_server']->push($fd,json_encode($data));
}
return Util::show(config('code.success'),'success',$data);
}
public function hello($name = 'ThinkPHP5')
{
return 'hello,' . $name;
}
public function sms(){
try{
$res = Sms::sendSms(15669762297,123456);
dump($res);
}catch(\Exception $e){
echo $e->getMessage();
}
}
}
<?php
class Ws{
CONST HOST = "0.0.0.0";
CONST PORT = 9501;
CONST CHART_PORT = 8812;
public $ws = null;
public function __construct(){
$this->ws = new swoole_websocket_server(self::HOST,self::PORT);
$this->ws->listen(self::HOST,self::CHART_PORT,SWOOLE_SOCK_TCP);
$this->ws->set([
'document_root' => "/home/root/default/swoole/public/static",
'enable_static_handler' => true,
'worker_num' => 5,
'task_worker_num' =>4
]);
$this->ws->on('start',[$this,'onStart']);
$this->ws->on('workerstart',[$this,'onWorkerStart']);
$this->ws->on('request',[$this,'onRequest']);
$this->ws->on('open',[$this,'onOpen']);
$this->ws->on('message',[$this,'onMessage']);
$this->ws->on('task',[$this,'onTask']);
$this->ws->on('finish',[$this,'onFinish']);
$this->ws->on('close',[$this,'onClose']);
$this->ws->start();
}
public function onWorkerStart(swoole_server $server,$worker_id){
define("APP_PATH",__DIR__."/../application/");
require __DIR__."/../thinkphp/start.php";
}
public function onStart($server){
//进程名字
swoole_set_process_name("live_master");
}
public function onRequest($request,$response){
//防止谷歌浏览器默认访问图标
if($request->server['request_uri'] =='/favicon.ico'){
$response->status(404);
$response->end();
return;
}
$_SERVER = [];
if(isset($request->server)){
foreach($request->server as $k=>$v){
$_SERVER[strtoupper($k)] = $v;
}
}
if(isset($request->header)){
foreach($request->header as $k=>$v){
$_SERVER[strtoupper($k)] = $v;
}
}
$_GET = [];
if(isset($request->get)){
foreach($request->get as $k=>$v){
$_GET[$k] = $v;
}
}
$_FILES = [];
if(isset($request->files)){
foreach($request->files as $k=>$v){
$_FILES[$k] = $v;
}
}
$_POST = [];
if(isset($request->post)){
foreach($request->post as $k=>$v){
$_POST[$k] = $v;
}
}
$_POST['http_server'] = $this->ws;
//缓冲区
ob_start();
try{
think\Container::get('app', [APP_PATH])
->run()
->send();
}catch(\Exception $e){
//todo
}
$res = ob_get_contents();
ob_end_clean();
$response->end($res);
}
public function onTask($serv,$taskId,$workerId,$data){
//分发task任务
$obj = new \app\common\lib\task\Task;
$method = $data['method'];
$flag = $obj->$method($data['data'],$serv);
return $flag;
}
public function onFinish($serv, $task_id, $data){
dump($data);
}
public function onOpen($ws,$req){
//fd作为有序集合放入redis中
var_dump($req->fd);
}
public function onMessage($ws,$frame){
echo $frame->fd;
$ws->push($frame->fd,"server-push");
}
public function onClose($ws,$fd){
var_dump($fd);
}
}
new Ws();
如果自己感兴趣可以参考以下视频教程自己亲自来搭建thinkphp5+swoole
地址:https://pan.baidu.com/s/1-y5h1oI99z2afxiqc7YCcA 提取码: huy9
2、搭建环境
条件不允许的情况下可以在自己的笔记本电脑上安装 vagrant+virturebox+lnmp+swoole,还有php的swoole拓展。(这一步就像自己搭建一个php的本地开发环境一样,自己亲手来搭建,这里就不做具体说明了)
3、开启服务
搭建好环境把第1步下载的文件放在你本地配置的虚拟机linux的自定义或者你想放的文件夹下。找到根目录的server这个文件夹下的ws.php文件,使用php ws.php命令来执行这个ws.php文件。
4、websocket请求
服务开启之后,在利用js写一个前端的websocket请求,我的本地html参考代码如下(ws://192.168.33.10:8812/?s=index/index/index这个地址可以根据你自己的请求url来更换)index.html
<html>
<head>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
// 初始化一个 WebSocket 对象
var wsObj = new WebSocket("ws://192.168.33.10:8812/?s=index/index/index"); //建立连接
wsObj.onopen = function(){ //发送请求
console.log("open");
wsObj.send("Hello WebSocket");
};
wsObj.onmessage = function(ev){ //获取后端响应
console.log(ev.data);
};
wsObj.onclose = function(ev){
alert("close");
};
wsObj.onerror = function(ev){
alert("error");
};
</script>
</body>
</html>
5、ajax正常请求
在写一个前端请求后端接口的 ajax请求html,我的请求代码如下 request.html
<html>
<head>
</head>
<body>
<button onclick='request()'>提交</button>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function request(){
$.ajax({
type:"POST", //请求方式
url:"http://192.168.33.10:9501/index/index/index",
dataType:"jsonp", //跨域json请求一定是jsonp
jsonp: "callbackparam", //跨域请求的参数名,默认是callback
data:{'content':'ceshi','game_id':1}, //请求参数
beforeSend: function() {
//请求前的处理
},
success: function(data) {
console.log(data);
//请求成功处理,和本地回调完全一样
},
complete: function() {
//请求完成的处理
},
error: function() {
//请求出错处理
}
});
}
</script>
</html>
6、验证
验证第5步ajax请求之后,websocket连接的页面是否会收到ajax请求传送的数据
(1)打开index.html
(2)打开request.html
(3)打开index.html查看