这次写的不依赖框架,不用nginx配置,只是依靠swoole的http_server和websocket,也是用的多端口监听,一边实现了web端的访问,一边实现了socket的发送;
文档在这里,可以多认真的读一下:https://wiki.swoole.com/wiki/page/525.html
好的,直接上码;
http.php
<?php
namespace app\common;
require_once 'Predis.php';
require_once 'Task.php';
/**
* socket面向对象的编译
*/
class Http
{
CONST HOST='0.0.0.0';
CONST PORT='9501';
public $ws=null;
public $http_server=null;
public function __construct()
{
$this->ws=new \swoole_websocket_server(self::HOST,self::PORT);
$this->ws->set([
//心跳检测
// 'heartbeat_check_interval' => 5,
// 'heartbeat_idle_time' => 10,
]);
//监听新端口
$this->http_server=$this->ws->listen("0.0.0.0", 9502, SWOOLE_SOCK_TCP);
//开启http模式
$this->http_server->set([
'open_http_protocol' => true,
]);
$this->ws->on("start", [$this, 'onStart']);
$this->http_server->on("request", [$this, 'onRequest']);
$this->ws->on('message',[$this,'onmessage']);
$this->ws->on('close',[$this,&#