业务逻辑图:
环境配置
swoole 4.4
swoole安装文档
php 7.2
nginx 1.16
服务端逻辑实现
文件目录结构
public文件夹中的serverStart.php为启动程序,其中包含数据库配置信息;swoole.log为日志记录
src文件夹中blueServer.php为服务端代码实现
vender中主要用到的是composer安装的laravel常用模型 Eloquent ORM,方便接收数据后的数据库操作。安装见:独立使用模型 Eloquent ORM。
下面见代码:
serverStart.php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Swoole\blueServer;
// date_default_timezone_set('PRC');
$opt = [
'daemonize' => false //true挂后台运行,false时可显示代码输出信息
];
$ws = new blueServer($opt);
//你的数据库配置信息
$database = [
'driver' => 'mysql',
'host' => 'rm-2zem***********sql.rds.aliyuncs.com',
'database' => 'test01',
'username' => 'root',
'password' => 'testtest',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
];
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
//创建一个数据库的链接
$capsule->addConnection($database);
//静态可访问
$capsule->setAsGlobal();
//启动Eloquent,实际上就是解析链接信息,开始建立数据库的链接
$capsule->bootEloquent();
$ws->start();
blueServer.php(TCP版本)
TCP服务端创建文档
<?php
namespace Swoole;
use swoole_server;
use Illuminate\Database\Capsule\Manager as DB;
//设置中国时区
date_default_timezone_set('PRC');
class blueServer
{
protected $ws;
//你的服务期地址
protected $host = 'ws://39.***.***.**';
//端口号
protected $port = 9506;
// 进程名称
protected $taskName = 'swooleBlue';
// PID路径
protected $pidFile = 'swooleBlue.pid';
// 设置运行时参数
protected $options = [
'worker_num' => 4, // worker进程数,一般设置为CPU数的1-4倍
'daemonize' => false, // 启用守护进程
'log_file' => 'swoole.log', // 指定swoole错误日志文件
'log_level' => 3, // 日志级别 范围是0-5,0-DEBUG,1-TRACE,2-INFO,3-NOTICE,4-WARNING,5-ERROR
'dispatch_mode' => 1 // 数据包分发策略,1-轮询模式
];
public function __construct($options = [])
{
$this->ws = new swoole_server($this->host, $this->port);
if (! empty($options)) {
$this->options = array_merge($this->options, $options);
}
//set方法必须在start前面
$this->ws->set($this->options);
$this->ws->on("connect", [$this, 'onConnect']);
$this->ws->on("receive", [$this, 'onReceive']);
$this->ws->on("close", [$this, 'onClose']);
}
public function start()
{
// Run worker
$this->ws->start();
}
public function onConnect(swoole_server $ws, $request)
{
// 设置进程名
cli_set_process_title($this->taskName);
// 记录进程id,脚本实现自动重启
$pid = "{$ws->master_pid}" . PHP_EOL . "{$ws->manager_pid}";
file_put_contents($this->pidFile, $pid);
echo "server: handshake success with fd{$request->fd}" . PHP_EOL;
}
public function onReceive(swoole_server $ws, $fd, $fromid, $data)
{
//接受数据,不同设备协议和数据格式都不同,我这里接收到的是二进制数据,所以必须先处理成16进制显示
//得到16进制的msg
$msg = bin2hex($data);
//拿到数据执行自己的业务逻辑,根据硬件数据文档过滤出自己需要的数据
//拿到自己需要的数据格式
//得到的数据存入数据库,NOSQL的话同理,自行搭建redis或者其他处理
$insert = DB::table('blue_test')->insert(['data' => $msg]);
}
public function onClose($ws, $fid)
{
echo "client {$fid} closed";
}
}
blueServer.php(UDP版本)
UDP服务端创建文档
<?php
namespace Swoole;
use swoole_server;
use Illuminate\Database\Capsule\Manager as DB;
class blueServer
{
protected $ws;
//服务器地址
protected $host = 'ws://39.***.***.**';
//端口
protected $port = 9503;
// 进程名称
protected $taskName = 'swooleBlue';
// PID路径
protected $pidFile = 'swooleBlue.pid';
// 设置运行时参数
protected $options = [
'worker_num' => 4, // worker进程数,一般设置为CPU数的1-4倍
'daemonize' => false, // 启用守护进程
'log_file' => 'swoole.log', // 指定swoole错误日志文件
'log_level' => 3, // 日志级别 范围是0-5,0-DEBUG,1-TRACE,2-INFO,3-NOTICE,4-WARNING,5-ERROR
'dispatch_mode' => 1 // 数据包分发策略,1-轮询模式
];
public function __construct($options = [])
{
//此处最后一个参数与上面TCP区分开
$this->ws = new swoole_server($this->host, $this->port, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
if (! empty($options)) {
$this->options = array_merge($this->options, $options);
}
//set方法必须在start前面
$this->ws->set($this->options);
//UDP
$this->ws->on("Packet", [$this, 'onPacket']);
}
public function start()
{
// Run worker
$this->ws->start();
}
//UDP接收数据的方法
public function onPacket(swoole_server $ws, $data, $clien_info)
{
//同样的,硬件一般以二进制格式传输数据,故先进制转换才能得到可读的数据
//得到16进制的msg
$msg = bin2hex($data);
//业务逻辑处理
//业务逻辑处理end
//end后数据存储,NOSQL的话同理,自行搭建redis或者其他
$insert = DB::table('blue_test2')->insert([data => $msg]);
}
}
代码逻辑完成后,将此项目三个文件夹(public、src、vender)提交你的服务器项目执行文件夹下,然后cd到public文件夹中,输入:php startServer.php
[root@iZ8vbakk9gu0d9ldq34aeaZ public]# php startServer.php
如果这里没有报错,但是你的数据库中又没有你想要的的数据,那么可以考虑是不是硬件设备配置后台的地址没有调通,这个根据硬件使用配置文档自己解决,就不一一赘述了。
完结。