Repository navigation
Conversation
Added a method to get a UdpConnection for proactive sending.
Worker::getUdpSender() returns a reusable UdpConnection bound to the worker's listening socket, for use in onWorkerStart / timers / channel callbacks. UdpConnection::sendTo($ip, $port, $data, $raw = false) sends to an arbitrary destination from that socket. UdpConnection::__construct() now defaults $remoteAddress to '' so a sender-only connection can be created without one.
Added a UDP server example to the README.
|
This PR adds the ability for a UDP worker to proactively send packets on its listening port, without waiting for an incoming packet. Two new methods are added: UdpConnection::sendTo(string $ip, int $port, mixed $data, bool $raw = false): bool Worker::getUdpSender(): ?UdpConnection Additionally, UdpConnection::__construct() now defaults $remoteAddress to '', so a sender-only connection can be created without a remote address. A new example in README snippet are included. 本 PR 让 UDP Worker 可以用自己的监听端口主动发数据,而不必等收到包再回复。新增两个方法: UdpConnection::sendTo(string $ip, int $port, mixed $data, bool $raw = false): bool Worker::getUdpSender(): ?UdpConnection 此外,UdpConnection::__construct() 的 $remoteAddress 参数加了默认值 '',这样"只用来发送"的连接不需要远端地址就能构造。同时补充了示例的 README 片段。 |
What
Add UdpConnection::sendTo(string $ip, int $port, mixed $data, bool $raw = false) for sending to an arbitrary destination.
Add Worker::getUdpSender(): ?UdpConnection to obtain a reusable sender bound to the listening socket.
Default $remoteAddress to '' in UdpConnection::__construct() to allow creating a sender-only connection.
Add an udp server example and update README.
Why
UdpConnection::send() only replies to the address of the incoming packet. Protocols like SIP/GB28181 require sending requests from the listening port (so the source port matches), validated by the peer. Currently, users must manually create a second socket. sendTo() and getUdpSender() fill this gap elegantly, keeping the logic inside the official UdpConnection without touching send()'s signature.
Why not modify send()
Adding a third parameter to send() would break signature compatibility for AsyncUdpConnection subclasses. A dedicated sendTo() is also clearer: send() replies, sendTo() targets. Placing it on UdpConnection makes the UDP-only nature obvious, as TcpConnection has no such concept.
Backward compatibility
Zero breakage. send() is untouched. The constructor change only adds a default value.