本文整理汇总了PHP中swoole_server::addlistener方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::addlistener方法的具体用法?PHP swoole_server::addlistener怎么用?PHP swoole_server::addlistener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::addlistener方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: initListener
protected function initListener()
{
$listeners = Application::AutoBuildCollection("Listener.php", IListener::class);
(yield array_shift($listeners));
/** @var IListener $listener */
foreach ($listeners as $listener) {
$port = $this->swoole->addlistener($listener->GetHost(), $listener->GetPort(), $listener->GetType());
if (is_array($listener->GetSetting())) {
$port->set($listener->GetSetting());
}
$this->_listenerConfigs[get_class($listener)] = ['Host' => $listener->GetHost(), 'Port' => $listener->GetPort(), 'Type' => $this->_getTypeName($listener->GetType())];
}
}
示例2: my_process1
return false;
}
self::$buffers[$fd] = new swoole_buffer(1024 * 128);
}
return self::$buffers[$fd];
}
}
if (isset($argv[1]) and $argv[1] == 'daemon') {
$config['daemonize'] = true;
} else {
$config['daemonize'] = false;
}
//$mode = SWOOLE_BASE;
$mode = SWOOLE_PROCESS;
$serv = new swoole_server("0.0.0.0", 9501, $mode);
$serv->addlistener('0.0.0.0', 9502, SWOOLE_SOCK_UDP);
$serv->addlistener('::', 9503, SWOOLE_SOCK_TCP6);
$serv->addlistener('::', 9504, SWOOLE_SOCK_UDP6);
$process1 = new swoole_process("my_process1", true, false);
$serv->addprocess($process1);
$serv->set(G::$config);
/**
* 保存数据到对象属性,在任意位置均可访问
*/
$serv->config = $config;
/**
* 使用类的静态属性,可以直接访问
*/
G::$serv = $serv;
function my_process1($process)
{
示例3: function
<?php
// Swoole提供了多端口监听的机制,这样可以同时监听UDP和TCP,同时监听内网地址和外网地址。内网地址和端口用于管理,外网地址用于对外服务。
$serv = new swoole_server("0.0.0.0", 9501);
$serv->set(array(
'daemonize' => 1, //
'heartbeat_idle_time' => 600,//连接如果600秒内未向服务器发送任何数据,此连接将被强制关闭
'heartbeat_check_interval' => 60,//每60秒遍历一次
));
//这里监听了一个UDP端口用来做内网管理
$serv->addlistener('127.0.0.1', 9502, SWOOLE_SOCK_UDP);
$serv->on('connect', function ($serv, $fd) {
echo "Client:Connect.\n";
// $serv->send($fd, 'server:hello world!');
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$info = $serv->connection_info($fd, $from_id);
// var_dump($info);
// $data=trim($data);
if (strlen($data)<5) {
return;
}
echo 'receive:'.$data;
//来自9502的内网管理端口
if($info['server_port'] == 9502) {
$serv->send($fd, "welcome admin2\n");
示例4: broadcast
<?php
// Swoole提供了多端口监听的机制,这样可以同时监听UDP和TCP,同时监听内网地址和外网地址。内网地址和端口用于管理,外网地址用于对外服务。
$serv = new swoole_server("0.0.0.0", 9501);
$serv->set(array('daemonize' => 1, 'heartbeat_idle_time' => 600, 'heartbeat_check_interval' => 60));
//这里监听了一个UDP端口用来做内网管理
$serv->addlistener('0.0.0.0', 9502, SWOOLE_SOCK_UDP);
$serv->on('connect', function ($serv, $fd) {
echo "Client:Connect.\n";
// $serv->send($fd, 'server:hello world!');
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$info = $serv->connection_info($fd, $from_id);
// var_dump($info);
// $data=trim($data);
if (strlen($data) < 5) {
return;
}
echo 'receive:' . $data;
//来自9502的内网管理端口
if ($info['server_port'] == 9502) {
$serv->send($fd, "welcome admin2\n");
} else {
broadcast($serv, $data);
// $serv->send($fd, $data);
}
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start();
function broadcast($serv, $msg)
示例5: run
public function run($ip = "0.0.0.0", $port = 55656, $mode = SWOOLE_PROCESS, $type = SWOOLE_SOCK_UDP)
{
if (empty($port)) {
$port = $this->handleWorkerPort;
} else {
$this->handleWorkerPort = $port;
}
$serv = new \swoole_server($ip, $port, $mode, $type);
//处理客户端发送的数据
$serv->addlistener('0.0.0.0', $this->handleProviderPort, SWOOLE_SOCK_TCP | SWOOLE_SOCK_UDP);
//处理统计页面请求的数据
$serv->addlistener('0.0.0.0', $this->udpFinderport, SWOOLE_SOCK_UDP);
//recv udp broadcast
$serv->config = \Config\Server::getServerConfig();
$serv->set($serv->config);
$serv->on('Start', array($this, 'onStart'));
$serv->on('WorkerStart', array($this, 'onWorkerStart'));
$serv->on('Connect', array($this, 'onConnect'));
$serv->on('Receive', array($this, 'onReceive'));
$serv->on('Task', array($this, 'onTask'));
$serv->on('Finish', array($this, 'onFinish'));
$serv->on('WorkerError', array($this, 'onWorkerError'));
$serv->on('Close', array($this, 'onClose'));
$serv->on('WorkerStop', array($this, 'onWorkerStop'));
$serv->on('Shutdown', array($this, 'onShutdown'));
$serv->on('ManagerStart', function ($serv) {
global $argv;
swoole_set_process_name("php {$argv[0]}: manager");
});
$serv->start();
}
示例6: addListener
function addListener($host, $port, $type)
{
return $this->sw->addlistener($host, $port, $type);
}
示例7: function
<?php
$serv = new swoole_server("0.0.0.0", 843);
$serv->addlistener('0.0.0.0', 11843, SWOOLE_SOCK_TCP);
echo "Flash 843 Server v 0.1 By 709653949@qq.com admin at 11843 start ....\n";
$serv->set(array('worker_num' => 1, 'daemonize' => false));
$serv->on('connect', function ($serv, $fd) {
$fdinfo = $serv->connection_info($fd);
$clientinfo = "Client ip:" . $fdinfo["remote_ip"] . " port:" . $fdinfo["remote_port"] . " to fromport:" . $fdinfo["from_port"] . " Connected!\n";
echo $clientinfo;
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$info = $serv->connection_info($fd, $from_id);
$recvdata = $data;
if ($info['from_port'] == 843) {
if (strpos("<policy-file-request/>", $recvdata) != 0) {
$serv->close($fd);
} else {
$response = "<?xml version=\"1.0\"?> \r\n<cross-domain-policy> \r\n\t<site-control permitted-cross-domain-policies=\"all\"/>\r\n\t<allow-access-from domain=\"*\" to-ports=\"*\"/>\r\n</cross-domain-policy>";
$serv->send($fd, $response);
echo "Send 843 ok!\n";
//.$response."\n";
$serv->close($fd);
}
} else {
echo "Close client data:" . $data . " connect ok!\n";
$serv->close($fd);
}
});
$serv->on('close', function ($serv, $fd) {
$fdinfo = $serv->connection_info($fd);
$clientinfo = "Client ip:" . $fdinfo["remote_ip"] . " port:" . $fdinfo["remote_port"] . " Closed.......\n\n\n";
示例8: dirname
<?php
//$serv = new swoole_server("0.0.0.0", 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$serv = new swoole_server("0.0.0.0", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$key_dir = dirname(dirname(__DIR__)) . '/tests/ssl';
$port2 = $serv->addlistener('0.0.0.0', 9502, SWOOLE_SOCK_TCP);
$port2->on('receive', function ($serv, $fd, $from_id, $data) {
echo "port2: " . $data . "\n";
});
$serv->set(array('ssl_cert_file' => __DIR__ . '/corpssl.crt', 'ssl_key_file' => __DIR__ . '/corpssl.key', 'ssl_client_cert_file' => __DIR__ . '/ca.crt', 'ssl_verify_depth' => 10));
$serv->on('connect', function (swoole_server $serv, $fd, $from_id) {
echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Connect.\n";
$info = $serv->getClientInfo($fd);
var_dump($info);
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
echo "[#" . posix_getpid() . "]\tClient[{$fd}]: {$data}\n";
$serv->send($fd, "Swoole: {$data}\n");
});
$serv->on('close', function ($serv, $fd, $from_id) {
echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Close.\n";
});
$serv->start();