/**
  * 初始化swoole服务配置
  * @param $ini_file swoole的服务启动配置文件位置
  */
 public function __construct($ini_file, $pidFile = false)
 {
     if ($pidFile) {
         $this->pidFile = $pidFile;
     }
     if (!is_file($ini_file)) {
         exit("Swoole AppServer配置文件错误({$ini_file})\n");
     }
     $config = parse_ini_file($ini_file, true);
     if (!is_array($this->config)) {
         $this->config = array();
     }
     $this->config = array_merge($this->config, $config);
     if (isset($this->config['server']['host']) && isset($this->config['server']['port'])) {
         parent::__construct($this->config['server']['host'] . ':' . $this->config['server']['port']);
     } else {
         parent::__construct(self::DEFAULT_HOST . ':' . self::DEFAULT_PORT);
     }
 }
Example #2
0
<?php

require_once "../vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name)
{
    return "Hello {$name}!";
}
$server = new Server("ws://0.0.0.0:8088");
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled();
$server->addFunction('hello');
$server->start();
Example #3
0
<?php

require_once "../vendor/autoload.php";
use Hprose\Swoole\Server;
use Hprose\Future;
$server = new Server("http://0.0.0.0:1315");
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled();
$server->addFunction(function ($a, $b) use($server) {
    $promise = new Future();
    swoole_timer_after(1000, function () use($a, $b, $promise) {
        $promise->resolve($a + $b);
    });
    return $promise;
}, "sum");
$server->start();
Example #4
0
<?php

require_once "../vendor/autoload.php";
use Hprose\Swoole\Server;
$server = new Server("tcp://0.0.0.0:2016");
$server->publish('time');
$server->on('workerStart', function ($serv) use($server) {
    $serv->tick(1000, function () use($server) {
        $server->push('time', microtime(true));
    });
});
$server->start();
Example #5
0
<?php

require_once "../vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name, $context)
{
    $context->clients->push("news", "this is a pushed message: {$name}");
    $context->clients->broadcast("news", array('x' => 1, 'y' => 2));
    $fdinfo = $context->server->connection_info($context->socket);
    return "Hello {$name}! -- {$fdinfo['remote_ip']}";
}
$server = new Server("tcp://0.0.0.0:1980");
$server->publish('news');
$server->onSubscribe = function ($topic, $id, $service) {
    error_log("client {$id} subscribe {$topic} on " . microtime(true));
};
$server->onUnsubscribe = function ($topic, $id, $service) {
    error_log("client {$id} unsubscribe {$topic} on " . microtime(true));
};
$server->addFunction('hello', array('passContext' => true));
$server->start();