本文整理汇总了PHP中swoole_server类的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server类的具体用法?PHP swoole_server怎么用?PHP swoole_server使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了swoole_server类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
//var_dump($serv->connection_info($fd, $from_id));
//echo "worker_pid=".posix_getpid().PHP_EOL;
//var_dump($fd, $from_id);
$serv->send($fd, 'Swoole: ' . $data, $from_id);
}
示例2: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message From Client {$fd}:{$data}\n";
$this->test = new Test();
var_dump($this->test);
$serv->task(serialize($this->test));
}
示例3: my_onTask
function my_onTask(swoole_server $serv, $task_id, $from_id, $data)
{
static $datas = array();
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case 'get':
$key = $data['key'];
$val = isset($datas[$key]) ? $datas[$key] : "";
$serv->finish(array('key' => $key, 'val' => $val));
break;
case "set":
$key = $data['key'];
$val = $data['val'] . "_" . $from_id;
$datas[$key] = $val;
return;
break;
case "del":
$key = $data['key'];
if (isset($datas[$key])) {
unset($datas[$key]);
}
break;
case "task":
$key = $data['key'];
echo "Do task " . $key . PHP_EOL;
break;
}
}
echo "AsyncTask[PID=" . posix_getpid() . "]: task_id={$task_id}." . PHP_EOL;
// $serv->finish("OK");
}
示例4: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message from client {$fd}:{$data}:{$from_id}\n";
$params = array('fd' => $fd, 'file' => $data);
$id = $serv->task(json_encode($params));
echo "Continue Handle Worker {$id}\n";
}
示例5: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message From Client {$fd}:{$data}\n";
$param = array('fd' => $fd, 'msg' => $data);
$str = json_encode($param);
$serv->after(1000, array($this, 'onAfter'), $str);
}
示例6: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$cmd = trim($data);
if ($cmd == "reload") {
$serv->reload($serv);
} elseif ($cmd == "task") {
$task_id = $serv->task("hello world", 0);
echo "Dispath AsyncTask: id={$task_id}\n";
} elseif ($cmd == "info") {
$info = $serv->connection_info($fd);
$serv->send($fd, 'Info: ' . var_export($info, true) . PHP_EOL);
} elseif ($cmd == "broadcast") {
$start_fd = 0;
while (true) {
$conn_list = $serv->connection_list($start_fd, 10);
if ($conn_list === false) {
break;
}
$start_fd = end($conn_list);
foreach ($conn_list as $conn) {
if ($conn === $fd) {
continue;
}
$serv->send($conn, "hello from {$fd}\n");
}
}
} elseif ($cmd == "error") {
hello_no_exists();
} elseif ($cmd == "shutdown") {
$serv->shutdown();
} else {
$serv->send($fd, 'Server Receive: ' . $data . PHP_EOL, $from_id);
$data = json_decode($data, true);
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case 'get':
$key = $data['key'];
$result = $serv->taskwait(json_encode($data), 0.5, 0);
$serv->send($fd, "get {$key}:" . $result . PHP_EOL);
break;
case "set":
$serv->task(json_encode($data), 0);
$serv->send($fd, "ok" . PHP_EOL);
break;
case "del":
$serv->task(json_encode($data), 0);
break;
case "reload":
break;
default:
echo "server:" . $data . PHP_EOL;
}
}
//$serv->close($fd);
}
//echo "Client:Data. fd=$fd|from_id=$from_id|data=$data";
//$serv->deltimer(800);
//swoole_server_send($serv, $other_fd, "Server: $data", $other_from_id);
}
示例7: process
public function process(swoole_server $serv, $taskId, $fromId, $data)
{
// model load
$this->defaultModel->sayHello();
// library load
$this->mcurl->isEnable();
$serv->finish('sync ok!');
}
示例8: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message From Client {$fd}:{$data}\n";
// send a task to task worker.
$param = array('fd' => $fd, 'source' => $data);
$serv->task(json_encode($param));
echo "Continue Handle Worker\n";
}
示例9: serve
function serve()
{
$serv = new \swoole_server(SERVERHOST, SERVERPORT);
$serv->on('workerStart', [$this, 'onStart']);
$serv->on('receive', [$this, 'onReceive']);
$serv->set(array('worker_num' => 1, 'dispatch_mode' => 1, 'open_length_check' => true, 'package_max_length' => 8192000, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4));
$serv->start();
}
示例10: __construct
public function __construct($host, $ip, $config, $bufferDriverClass)
{
$server = new \swoole_server($host, $ip, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$server->set($config);
$server->on('receive', array($this, 'onReceive'));
$server->on('close', array($this, 'onClose'));
$this->server =& $server;
$this->bufferDriverClass = $bufferDriverClass;
}
示例11: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$info = $serv->connection_info($fd, $from_id);
if ($info['server_port'] == 9502) {
$serv->send($fd, "welcom Admin \n");
} else {
$serv->send($fd, 'Swoole:' . $data);
}
}
示例12: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message From Client {$fd}:{$data}\n";
foreach ($serv->connections as $client) {
if ($fd != $client) {
$serv->send($client, $data);
}
}
}
示例13: onConnect
public function onConnect(swoole_server $serv, $fd, $from_id)
{
//获取连接的客户端信息
$fdInfo = $serv->connection_info($fd);
echo '<pre>';
print_r($fdInfo);
echo '</pre>';
$serv->send($fd, "Welcome {$fd} Connect Server");
}
示例14: run
function run()
{
$serv = new swoole_server("127.0.0.1", 8002);
$serv->set(array('timeout' => 1, 'poll_thread_num' => 1, 'worker_num' => 1, 'backlog' => 128, 'max_conn' => 10000, 'dispatch_mode' => 2));
$serv->on('Receive', array($this, 'onReceive'));
$serv->on('Close', array($this, 'onClose'));
//swoole_server_addtimer($serv, 2);
#swoole_server_addtimer($serv, 10);
$serv->start();
}
示例15: onReceive
function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$this->count[$fd]++;
echo "Client#{$fd} recv: {$data}\n";
if ($this->count[$fd] > 10) {
$serv->close($fd);
} else {
$serv->send($fd, "hello client");
}
}