本文整理汇总了PHP中swoole_server::listen方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::listen方法的具体用法?PHP swoole_server::listen怎么用?PHP swoole_server::listen使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::listen方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: function
<?php
$serv = new swoole_server('127.0.0.1', 9001);
for ($port = 9002; $port < 9999; $port++) {
$serv->listen("127.0.0.1", $port, SWOOLE_SOCK_TCP);
}
$serv->on("receive", function ($serv, $fd, $reactor_id, $data) {
$info = $serv->getClientInfo($fd);
var_dump($info);
});
$serv->start();
示例2: use
return false;
}
self::$buffers[$fd] = new swoole_buffer(1024 * 128);
}
return self::$buffers[$fd];
}
}
if (isset($argv[1]) and $argv[1] == 'daemon') {
G::$config['daemonize'] = true;
} else {
G::$config['daemonize'] = false;
}
$mode = SWOOLE_BASE;
//$mode = SWOOLE_PROCESS;
$serv = new swoole_server("0.0.0.0", 9501, $mode, SWOOLE_SOCK_TCP);
$serv->listen('0.0.0.0', 9502, SWOOLE_SOCK_UDP);
$serv->listen('::', 9503, SWOOLE_SOCK_TCP6);
$serv->listen('::', 9504, SWOOLE_SOCK_UDP6);
$process1 = new swoole_process(function ($worker) use($serv) {
global $argv;
swoole_set_process_name("php {$argv[0]}: my_process1");
swoole_timer_tick(2000, function ($interval) use($worker, $serv) {
echo "#{$worker->pid} child process timer {$interval}\n";
// 如果worker中没有定时器,则会输出 process timer xxx
foreach ($serv->connections as $conn) {
$serv->send($conn, "heartbeat\n");
}
});
swoole_timer_tick(5000, function () use($serv) {
$serv->sendMessage("hello event worker", 0);
$serv->sendMessage("hello task worker", 4);
示例3: array
<?php
class G
{
static $cluster = array();
}
$server = new swoole_server("0.0.0.0", 9501);
$server->listen("192.168.1.100", 9509, SWOOLE_NODE);
G::$cluster[] = $server->addNode("192.168.1.102", 9509);
G::$cluster[] = $server->delNode("192.168.1.103", 9509);
$server->on('NodeMessage', function (swoole_server $serv, $nodeId, $message) {
$replyMessage = "helo {$message}";
//回复消息
$serv->sendMessage($replyMessage, $nodeId);
//向第二个节点发消息
$serv->sendMessage($message, G::$cluster[1]);
//添加新节点
$cluster[] = $serv->addNode("192.168.1.103", 9509);
});
$server->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
$cmd = trim($data);
//发给节点0
$serv->sendMessage($cmd, G::$cluster[0]);
//向所有节点广播
foreach (G::$cluster as $nodeId) {
$serv->sendMessage($cmd, $nodeId);
}
});
$server->start();
示例4: function
<?php
$server = new swoole_server('0.0.0.0', 9905, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
for ($i = 0; $i < 20; $i++) {
$server->listen('0.0.0.0', 9906 + $i, SWOOLE_SOCK_UDP);
}
$server->set(['worker_num' => 4]);
$server->on('Packet', function (swoole_server $serv, $data, $addr) {
$serv->sendto($addr['address'], $addr['port'], "Swoole: {$data}", $addr['server_socket']);
});
$server->start();
示例5: function
<?php
$serv = new swoole_server("0.0.0.0", 9501);
$port = $serv->listen('127.0.0.1', 9502, SWOOLE_SOCK_UDP);
$port->on('packet', function ($serv, $data, $addr) {
var_dump($serv, $data, $addr);
});
$serv->on('connect', function ($serv, $fd, $from_id) {
echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Connect.\n";
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
echo "[#" . $serv->worker_id . "]\tClient[{$fd}]: {$data}\n";
if ($serv->send($fd, "hello\n") == false) {
echo "error\n";
}
});
$serv->on('close', function ($serv, $fd, $from_id) {
echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Close.\n";
});
$serv->start();