安装swoole扩展
安装php扩展swoole的两种方法
查看服务器是否开启Swoole PHP扩展
php -i | grep swoole
1、使用pecl命令安装
pecl install swoole
2、手动编译安装
使用以下命令下载swoole安装包
git clone https://github.com/swoole/swoole-src.git
解压源代码进入目录
tar -zxvf swoole-src.tar.gz
cd swoole-src
执行以下命令
phpize
./configure
make
make install
修改php.ini文件,在最后添加这句
extension=swoole.so
保存重启php-fpm ,执行以下命令
php -m
看到如下图所示,表示php扩展swoole已经安装好了
安装laravel-swoole
使用以下命令安装laravel扩展
composer require swooletw/laravel-swoole
在config/app.php的数组providers增加
SwooleTW\Http\LaravelServiceProvider::class
发布配置文件,并修改端口号为9052
php artisan vendor:publish --tag=laravel-swoole
Nginx反向代理
把url的请求转发到swoole服务地址127.0.0.1:9502
location /sw {
proxy_pass http://127.0.0.1:9052;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
rewrite /sw/(.*) /$1 break;
proxy_redirect off;
}
检查是否服务器是否开启防火墙,如果开启防火墙,服务器防火墙开放9052端口
使用示例
使用laravel自带的方法artisan
使用以下命令创建swoole
php artisan make:command Swoole
会在
app\Console\Command\
目录下生成Swoole
文件
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use swoole_websocket_server;
class Swoole extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'swoole {action}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
* 这个方法中的逻辑需要自己写
* @return mixed
*/
public function handle()
{
$action = $this->argument('action');
switch ($action) {
case 'close':
break;
default:
$this->start();
break;
}
}
/**
* 这个方法是自己新增的
* 具体可参考 https://wiki.swoole.com/#/start/start_tcp_server
*/
public function start()
{
// 这里是监听的服务端口号
$this->ws = new swoole_websocket_server("0.0.0.0", 9502);
//监听WebSocket连接打开事件
$this->ws->on('open', function ($ws, $request) {
});
//监听WebSocket消息事件
$this->ws->on('message', function ($ws, $frame) {
$this->info("client is SendMessage4545\n" . $frame);
});
//监听WebSocket主动推送消息事件
$this->ws->on('request', function ($request, $response) {
$scene = $request->post['scene'];
foreach ($this->ws->connections as $fd) {
if ($this->ws->isEstablished($fd)) {
$this->ws->push($fd, $scene);
}
}
});
//监听WebSocket连接关闭事件
$this->ws->on('close', function ($ws, $fd) {
$this->info("client is close\n");
});
$this->ws->start();
}
}
在
Kernel.php
文件中注册这个Swoole
类,
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
\App\Console\Commands\RedisSubscribe::class,
\App\Console\Commands\Swoole::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
在根目录下执行以下命令就会开启一个长进程
php artisan swoole start
如果上面的命令不生效则尝试以下命令
nohup php artisan swoole:http start &
重启swoole服务,请使用以下命令
php artisan swoole:http restart
如果上面命令重启不生效则尝试以下命令
nohup php artisan swoole:http restart &
停止swoole服务,请使用以下命令
php artisan swoole stop
如果上面命令不生效则尝试以下命令
nohup php artisan swoole:http stop &
前端页面实现长链接
<!doctype html>
<html>
<head>
<title>测试WebSocket</title>
</head>
<div id="WebSocket"></div>
<body>
<script>
var ws = new WebSocket("您的域名/sw");
ws.onopen = function(event) {
console.log("客户端已连接上!");
ws.send("hello server,this is client!"); //客户端给服务端推送消息
};
ws.onmessage = function(event) {
var parent = document.getElementById('WebSocket');
var div = document.createElement("div");
div.innerHTML = event.data
parent.appendChild(div);
console.log("服务器传过来的数据是:" + event.data);
}
ws.onclose = function(event) {
console.log("连接已关闭");
};
</script>
</body>
websocket
检测的是端口只要通过这个端口发送数据都会获取到。到此,larave-swoole
前后端长链接实现。希望可以帮到大家。