当前位置: 首页>>代码示例>>PHP>>正文


PHP swoole_server::listen方法代码示例

本文整理汇总了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();
开发者ID:noikiy,项目名称:swoole-src,代码行数:11,代码来源:listen_1k_port.php

示例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);
开发者ID:noikiy,项目名称:swoole-src,代码行数:31,代码来源:server.php

示例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();
开发者ID:pandabeta,项目名称:swoole-src,代码行数:29,代码来源:node_message.php

示例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();
开发者ID:google2013,项目名称:swoole-src,代码行数:11,代码来源:server.php

示例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();
开发者ID:noikiy,项目名称:swoole-src,代码行数:20,代码来源:echo.php


注:本文中的swoole_server::listen方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。