swoft socket
1.0.3
O componente socket
baseado em swoft suporta monitoramento de múltiplas portas tcp/udp
. Para obter detalhes, consulte o wiki do swoole.
composer require wp-breeder/swoft-socket
/project-to-path/config/properties/app
: // 配置启动扫描
' bootScan ' => [
// code...
// 把 sockerListener 注解的目录添加到启动扫描
' AppListener '
],
// 指定扫描组件
' components ' => [
' custom ' => [
' Swoft \ Socket \'
],
],
/project-to-path/config/server
: //for socket component
' server ' => [
//code ...
// socketable 配置是否开启socket监听
' socketable ' => env ( ' SOCKETABLE ' , false ),
],
// for socket component test
'socket' => [
[
' host ' => env ( ' SOCKET_TCP_HOST ' , ' 0.0.0.0 ' ),
' port ' => env ( ' SOCKET_TCP_PORT ' , 8010 ),
' mode ' => env ( ' SOCKET_TCP_MODE ' , SWOOLE_PROCESS ),
' type ' => env ( ' SOCKET_TCP_TYPE ' , SWOOLE_SOCK_TCP ),
//server 名称,必须设置,如果未设置则不监听(用于区分不同server 设置不同的回调,如果重复,则会覆盖)
//如果是单独开启,第一个不设置name则会报错
' name ' => ' tcp '
],
[
' host ' => env ( ' SOCKET_UDP_HOST ' , ' 0.0.0.0 ' ),
' port ' => env ( ' SOCKET_UDP_PORT ' , 8011 ),
' mode ' => env ( ' SOCKET_UDP_MODE ' , SWOOLE_PROCESS ),
' type ' => env ( ' SOCKET_UDP_TYPE ' , SWOOLE_SOCK_UDP ),
' name ' => ' udp '
],
],
rpc
e http
A nova anotação
@SocketListener
é adicionada. O método de uso é aproximadamente o mesmo que@ServerListener
.
/**
* 监听多个事件
* @SocketListener({
* SwooleEvent:: ON_CONNECT ,
* SwooleEvent:: ON_RECEIVE ,
* SwooleEvent:: ON_CLOSE ,
* },
* 监听端口的名称,与socket配置文件中的name对应(如重复则最后一个生效,不存在则监听不生效)
* name="tcp"
* )
rpc
, http
, ws
. (Se houver vários monitores, a primeira configuração será o servidor mestre por padrão e outras configurações monitorarão a porta especificada separadamente) php bin/swoft socket:start
namespace SwoftTest Socket Testing Listeners ;
use Swoft Bootstrap Listeners Interfaces CloseInterface ;
use Swoft Bootstrap Listeners Interfaces ConnectInterface ;
use Swoft Bootstrap Listeners Interfaces ReceiveInterface ;
use Swoft Socket Bean Annotation SocketListener ;
use Swoft Socket Event SwooleEvent ;
use Swoole Server ;
/**
* tcp 监听 demo
* Class SocketListenerTest
* @package SwoftTestSocketTestingListeners
* @SocketListener({
* SwooleEvent::ON_CONNECT,
* SwooleEvent::ON_RECEIVE,
* SwooleEvent::ON_CLOSE,
* },
* name="tcp"
* )
*/
class TcpListener implements ReceiveInterface, CloseInterface, ConnectInterface
{
public function onConnect ( Server $ server , int $ fd , int $ reactorId )
{
echo " tcp connect " . PHP_EOL ;
}
public function onReceive ( Server $ server , int $ fd , int $ reactorId , string $ data )
{
echo " tcp receive data: " . $ data . PHP_EOL ;
}
public function onClose ( Server $ server , int $ fd , int $ reactorId )
{
echo " tcp close " . PHP_EOL ;
}
}
namespace SwoftTest Socket Testing Listeners ;
use Swoft Socket Event SwooleEvent ;
use Swoft Bootstrap Listeners Interfaces PacketInterface ;
use Swoft Socket Bean Annotation SocketListener ;
use Swoole Server ;
/**
* udp 监听 demo
* Class PacketListener
* @package SwoftTestSocketTestingListeners
* @SocketListener(event={SwooleEvent::ON_PACKET}, name="udp")
*/
class PacketListener implements PacketInterface
{
public function onPacket ( Server $ server , string $ data , array $ clientInfo )
{
echo " udp receive data: " . $ data ;
}
}
O Componente é um software de código aberto licenciado sob a licença Apache.