鉴于 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去搭建TCP服务, 结果使用telnet命令测试不通?

第四步, 鉴于WebSocketService.php的设置, 将5291端口暴露出来(workspace、php-worker或0.0.0.0均测试过)

鉴于WebSocketService去搭建TCP服务, 结果使用telnet命令测试不通?

鉴于WebSocketService去搭建TCP服务, 结果使用telnet命令测试不通?

鉴于WebSocketService去搭建TCP服务, 结果使用telnet命令测试不通?

结果:

鉴于WebSocketService去搭建TCP服务, 结果使用telnet命令测试不通?

在服务器上使用netstat -an | grep 5291查看端口占用情况:

鉴于WebSocketService去搭建TCP服务, 结果使用telnet命令测试不通?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

修改/laradock/docker-compose.yml文件, 添加如图(即将9501暴露给外网,并映射到nginx内网的9501端口)

注意-前的缩进要用空格, 否则在执行docker-compose up -d --build --recreate xxxx时会报格式错误.

file 同时在/laradock/nginx/Dockerfile中的末尾的EXPOSE后添加9501端口 file

最后重新build容器即可.

3年前 评论
讨论数量: 5

@liyu001989 老师, 求帮忙 :grin:

3年前 评论
liyu001989

你是用的 laradock,5291 是 workspace 容器内部的端口,没有映射出来,所以你在外面访问不到。你应该配合 nginx 使用啊

3年前 评论

@liyu001989 也就是说, 我需要在nginx里配置相应的监听端口以及将相应的tcp转发至workspace 5291下嘛?

按照上面的理解, 首先我在/laradock/nginx/Dockerfile的最后添加了9501端口(云服务安全组也添加了该端口), 然后在/laradock/nginx/sites/my_site.conf 下追加下图配置, 并重新创建了容器(docker-compose up -d --build --force-recreate nginx php-worker workspace)

file 结果使用telnet命令依然Unable to connect to remote host :sob:

3年前 评论
liyu001989

有了nginx 直接用域名了啊,你看课程里面的 nginx 配置

3年前 评论

修改/laradock/docker-compose.yml文件, 添加如图(即将9501暴露给外网,并映射到nginx内网的9501端口)

注意-前的缩进要用空格, 否则在执行docker-compose up -d --build --recreate xxxx时会报格式错误.

file 同时在/laradock/nginx/Dockerfile中的末尾的EXPOSE后添加9501端口 file

最后重新build容器即可.

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!