KTVCentral/app/Services/TcpSocketClient.php
allen.yan 7c8c3fe69b DB 滙入
User 介面 只能看資料不能做修改
Role 介面移除
Branch介面 只能看資料不能做修改
Room 介面 可操控 包廂開關台
Sqgger API 可操控API
Room 有操控異動記錄
machine_statuses 需做資料留存需留7 天
20250528
2025-05-28 09:24:03 +08:00

43 lines
1.1 KiB
PHP

<?php
namespace App\Services;
class TcpSocketClient
{
protected $ip;
protected $port;
protected $timeout;
public function __construct(string $ip, int $port, int $timeout = 5)
{
$this->ip = $ip;
$this->port = $port;
$this->timeout = $timeout;
}
public function send(string $data): string
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
throw new \Exception("Socket create failed: " . socket_strerror(socket_last_error()));
}
$result = socket_connect($socket, $this->ip, $this->port);
if ($result === false) {
throw new \Exception("Socket connect failed: " . socket_strerror(socket_last_error($socket)));
}
socket_write($socket, $data, strlen($data));
$response = '';
while ($out = socket_read($socket, 2048)) {
$response .= $out;
// 根據協議判斷是否結束接收,可以自行調整
if (strpos($response, "\n") !== false) {
break;
}
}
socket_close($socket);
return $response;
}
}