鉴于 WebSocketService 去搭建 TCP 服务, 结果使用 telnet 命令测试不通?
根据
hhxsv5/laravel-s
文档一步步搭建TCP
服务, 结果无法访问!
第一步, 在app\Services
目录下新建TcpSocketSerivce.php
, 代码如下
<?php
namespace App\Services;
use Hhxsv5\LaravelS\Swoole\Socket\TcpSocket;
use Swoole\Server;
class TcpSocketService extends TcpSocket
{
public function onConnect(Server $server, $fd, $reactorId)
{
\Log::info('New TCP connection', [$fd]);
$server->send($fd, 'Welcome to LaravelS.');
}
public function onReceive(Server $server, $fd, $reactorId, $data)
{
\Log::info('Received data', [$fd, $data]);
$server->send($fd, 'LaravelS: ' . $data);
if ($data === "quit\r\n") {
$server->send($fd, 'LaravelS: bye' . PHP_EOL);
$server->close($fd);
}
}
public function onClose(Server $server, $fd, $reactorId)
{
\Log::info('Close TCP connection', [$fd]);
$server->send($fd, 'Goodbye');
}
}
第二步, 配置config/laravels.php
.
.
.
'sockets' => [
[
'host' => env('LARAVELS_TCP_HOST', '127.0.0.1'),
'port' => 5291,
'type' => SWOOLE_SOCK_TCP,// 支持的嵌套字类型:https://wiki.swoole.com/#/consts?id=socket-%e7%b1%bb%e5%9e%8b
'settings' => [// Swoole可用的配置项:https://wiki.swoole.com/#/server/port?id=%e5%8f%af%e9%80%89%e5%8f%82%e6%95%b0
'open_eof_check' => true,
'package_eof' => "\r\n",
],
'handler' => \App\Services\TcpSocketService::class,
],
],
第三步, 前往云服务器, 设置安全组中的端口
第四步, 鉴于WebSocketService.php
的设置, 将5291
端口暴露出来(workspace、php-worker或0.0.0.0均测试过)
结果:
在服务器上使用
netstat -an | grep 5291
查看端口占用情况:
推荐文章: