来自javascript客户端的Websocket服务器连接为net :: ERR_CONNECTION_REFUSED

时间:2020-07-25 16:00:46

标签: javascript php websocket clientwebsocket

我的域:: https://example.com

我的Web套接字服务器代码路径:: http://live.example.com

我的php客户端路径:: http://live.example.com/client-php

我的JS客户端路径:: http://live.example.com/client-web

我有如下运行的php websocket服务器

<?php
    namespace App;
    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    require_once './controllers/SocketController.php';

    require './vendor/autoload.php';

    // configurations
    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new SocketController()
            )
        ),
        8585 // the port of the web socket // I TIRED 8080,8181 also
    );

    $server->run();

我有两个客户端,一个是 php-client,它按如下方式连接到套接字

\Ratchet\Client\connect('ws://127.0.0.1:8585')->then(function($conn) {
                $conn->send(
                    json_encode(array(
                        "to" => "34366",
                        "data" => "Hello World",
                    ))
                );
                $conn->close();
            }, function ($e) {
                $this->logger->info("Can not connect: {$e->getMessage()}");
            });

运行上述代码后,websocket连接成功,我可以从我的socketcontroller中看到新的连接日志,如下所示:

新连接! (51)连接51已断开连接

接下来,我有一个 javascript网络客户端,它也可以如下连接到同一网络套接字

var socket = new WebSocket('ws://localhost:8585');
socket.onopen = function (event) {
    log.append('Conect to server <br>');
    socket.send('{ "connect": "1", "id": "34366" }');
};

但是在运行Web客户端控制台时,始终给出以下错误

index.js:2 WebSocket connection to 'ws://localhost:8585/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
(anonymous) @ index.js:2

在下面的行中有问题吗?

新的WebSocket('ws:// localhost:8585');

我也尝试给出127.0.0.1,localhost,domain,socketserver路径的路径,但是总是出现相同的错误。

请帮助!

0 个答案:

没有答案