0

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

我的网络套接字服务器代码路径 :: 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 Web 客户端,它也连接到相同的 Web 套接字,如下所示

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 路径提供路径,但总是出现相同的错误。

请帮忙!!

4

0 回答 0