[ERROR] stream_socket_client(): unable to connect to tcp://127.0.0.1:2902 (Connection refused)

phper1

PHP 框架使用的是 codeigniter4,前端用的是 uniapp,gatewayworker 从官网下载的 linux 版本,gatewayclient 使用composer 安装。gatewayWorke、gatewayclient 和应用使用同一服务器。

App/Gatewayworker/Applications/YourApp/start.gateway.php中只做了一处修改:将原来的:

$gateway = new Gateway(" tcp://0.0.0.0:8282");

改成了 websocket 协议:

$gateway = new Gateway("websocket://0.0.0.0:8282"); ```

App/Gatewayworker/Applications/YourApp/Events.php中,只对 onConnect 做了以下修改:

public static function onConnect($client_id)
{
Gateway::sendToClient($client_id, json_encode(array(
'type' => 'init',
'client_id' => $client_id
)));
// 向当前client_id发送数据
Gateway::sendToClient($client_id, "Hello $client_id\r\n");
// 向所有人发送
Gateway::sendToAll("$client_id login\r\n");
}```

前端使用 websocket 连接,在收到'init'和 client_id的信息后,通过 bind_uid 接口向后台上传 uid 和 client_id 两个参数。

后台接收前端上传的 uid 和 client_id 两个参数后,进行绑定。后台代码如下:

<?php namespace App\Controllers;
require_once '../vendor/autoload.php';
use CodeIgniter\Controller;
use App\Models\Push_model;
use GatewayClient\Gateway;

Class Push2 extends Controller
{
    public function __construct()
    {
        Gateway::$registerAddress = '127.0.0.1:1238';
    }

    public function bind_uid()
    {
        $uid       = $this->request->getPost('uid');
        $client_id = $this->request->getPost('client_id');
        try
        {
            Gateway::$registerAddress = '127.0.0.1:1238';
            Gateway::bindUid($client_id, $uid);
            $message = "binding is fixed";
            Gateway::sendToUid($uid, json_encode($message));
}
        catch (\Exception $e)
        {
            log_message('error', '[ERROR] {exception}', ['exception' => $e]);
        }
    }

问题:
1.前端上传后,收不到返回的消息
2.打印错误日志,报如下内容:

(Connection refused) /srv/vendor/workerman/gatewayclient/Gateway.php:1187
ERROR - 2021-08-28 08:28:03 --> [ERROR] stream_socket_client(): unable to connect to tcp://127.0.0.1:2903 (Connection refused) /srv/vendor/workerman/gatewayclient/Gateway.php:1188

已经做过的尝试:
1.服务器端口放行1238、2900、8282
2.重启过 gateway 服务器
以上调整没有效果。

请高手指点。十分期待,感谢感谢!!!

4518 3 0
3个回答

six

服务端debug运行,看下是不是有报错。
或者运行php start.php status看

  • phper1 2021-08-29

    谢谢回复。

    服务端 debug 模式运行,不输出错误。
    服务端 php start.php status,提示:

    Workerman[start.php] status 
    Workerman[start.php] not run
phper1

谢谢回复。

服务端 debug 模式运行,不输出错误。
服务端 php start.php status,提示:

Workerman[start.php] status 
Workerman[start.php] not run
  • 暂无评论
six

咋会not run,not run就是没启动啊

  • phper1 2021-08-30

    是的,我目前的排除方向,是不是因为我用 docker,导致容器相互隔绝造成的。开放了端口我再测试一下。谢谢你~~

  • phper1 2021-08-31

    一圈下来,问题仍然没有解决。php.start status 输出 not run. 那这个没有启动的原因是什么呢?该怎么解决呢?

  • six 2021-09-04

    不用docker试下呢,排除docker问题

年代过于久远,无法发表回答
🔝