<?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();
<?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();
<?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();