swoole和erlang通信测试

直接用docker跑环境

docker pull xlight/docker-php7-swoole
docker run -it -v ~/Projects/php/swoole:/workdir -p 9501:9501 xlight/docker-php7-swoole /bin/bash
php workdir/app_server.php start

 

app_server.php

<?php

$serv = new swoole_server("0.0.0.0", 9501);

$serv->set(array(
    'package_max_length' => 8192,
    'open_length_check'=> true,
    'package_length_offset' => 0,

    //大小端默认,组包方式{packet, 2}
    // 'package_body_offset' => 2,
    // 'package_length_type' => 'n'

    //大小端默认,组包方式{packet, 4}
    'package_body_offset' => 4,
    'package_length_type' => 'N'
));

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

 

参数参考地址

 

posted @ 2016-08-16 10:59  自由出土文物  阅读(573)  评论(0编辑  收藏  举报