【发布时间】:2019-02-04 02:42:22
【问题描述】:
我正在尝试从游戏中接收 UDP 数据。并在页面上不断更新。使用 JSON 从函数中检索数据。
是否可以将 socket_create/socket_bind 与 socket_recvfrom 分开?
(我已经删掉了一些不必要的代码)
private $socket;
public function socketConnect($port)
{
$ip = '0.0.0.0'; // local IP
$port = 20777; // port to listen
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($this->socket, $ip, $port) or die("Could not connect");
return view('telemetry');
}
public function getData()
{
while (socket_recvfrom($this->socket, $buf, $bytes, 0, $remote_ip, $remote_port)) {
// do something
return response()->json($data);
}
}
我尝试设置 $this->socket 来保留数据,但这似乎不起作用。
【问题讨论】: