Websocket server connection from javascript client gives net::ERR_CONNECTION_REFUSED

271 views Asked by At

My domain :: https://example.com

My web socket server code path :: http://live.example.com

My php client path:: http://live.example.com/client-php

My JS client path :: http://live.example.com/client-web

I have my php websocket server running as follows

<?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();

I have two client, one is php-client , which connects to socket as follows

\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()}");
            });

After above code is run , websocket connection is successful ,i can see new connection logs from my socketcontroller as follows :

New connection! (51) Connection 51 has disconnected

Next I have one javascript web client also that connects to same web socket as follows

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

But on running the web client console give below error always

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

Do below line have issue ??

new WebSocket('ws://localhost:8585');

I tried giving paths for 127.0.0.1, localhost, domain , socketserver path also , but same errors always.

Please help!!

0

There are 0 answers