Created
December 6, 2019 12:20
-
-
Save diyism/e9880a3307d1a953d012bf9d333c5bc1 to your computer and use it in GitHub Desktop.
create multiple servers with php swoole
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
open two udp servers and two tcp servers simultaneously with swoole | |
to test them from linux shell: | |
$nc -u 127.0.0.1 21404 | |
helloooo_udp | |
helloooo_udp | |
... | |
$nc -u 127.0.0.1 21405 | |
helliiiii_udp | |
helliiiii_udp | |
... | |
$nc 127.0.0.1 21406 | |
hellaaaaa_tcp | |
hellaaaaa_tcp | |
... | |
$nc 127.0.0.1 21407 | |
helleeeee_tcp | |
helleeeee_tcp | |
... | |
*/ | |
$udp_server=new swoole_server('0.0.0.0', 21404, SWOOLE_BASE, SWOOLE_UDP); | |
$udp_server->on('packet', function ($ipsock, $data, $ipsock_con) | |
{ | |
$ipsock->sendto($ipsock_con['address'], $ipsock_con['port'], 'helloooo_udp', $ipsock_con['server_socket']); | |
} | |
); | |
$udp_server1=$udp_server->listen('0.0.0.0', 21405, SWOOLE_UDP); | |
$udp_server1->on('packet', function ($ipsock, $data, $ipsock_con) | |
{ | |
$ipsock->sendto($ipsock_con['address'], $ipsock_con['port'], 'helliiiii_udp', $ipsock_con['server_socket']); | |
} | |
); | |
$tcp_server=$udp_server->listen('0.0.0.0', 21406, SWOOLE_TCP); | |
$tcp_server->on('receive', function ($server, $fd, $reactor_id, $data) | |
{ | |
$server->send($fd, 'hellaaaaa_tcp'); | |
} | |
); | |
$tcp_server1=$udp_server->listen('0.0.0.0', 21407, SWOOLE_TCP); | |
$tcp_server1->on('receive', function ($server, $fd, $reactor_id, $data) | |
{ | |
$server->send($fd, 'helleeeee_tcp'); | |
} | |
); | |
$udp_server->start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment