websocket Fatal error message stating "Failed to listen on tcp://0.0.0.0:8080: Address already in use

38 views Asked by At

I am encountering a Fatal error message stating "Failed to listen on tcp://0.0.0.0:8080: Address already in use" when trying to create a server for websockets on my website. How can I resolve this issue?

Problem: I am trying to set up a websocket server on my website, but I am receiving the following error message: "Fatal error: Uncaught RuntimeException: Failed to listen on "tcp://0.0.0.0:8080": Address already in use." This error is occurring in the TcpServer.php file on line 184. How can I troubleshoot and fix this issue to successfully set up the websocket server on my website?

I'm trying to make real-time send data between admin python and the python clients throw php server

this is admin python script (Sender)

import asyncio
import websockets
import json

async def send_array():
    uri = "ws://example.com:8080"
    async with websockets.connect(uri, timeout=60.0) as websocket:
        array_data = [1, 2, 3, "hello"]
        await websocket.send(json.dumps(array_data))

asyncio.run(send_array())

and the error when i run it it's

raise TimeoutError from exc_val TimeoutError

this is the php file server

require_once 'vendor/autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class WebSocketServer implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $data = json_decode($msg, true);

        if (is_array($data)) {
            // Process the array data as needed
            echo "Received array data: " . print_r($data, true) . "\n";

            // Send the array data to all connected clients
            foreach ($this->clients as $client) {
                if ($from !== $client) {
                    $client->send(json_encode($data));
                }
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}

php file error is

Fatal error message stating "Failed to listen on tcp://0.0.0.0:8080: Address already in use

and this is the python client (Receiver)

import asyncio
import json
import websockets

# Replace this URL with the URL of your PHP server
url = "ws://example:8080"

async def receive_array():
    async with websockets.connect(url) as websocket:
        while True:
            # Receive data from the server
            data = await websocket.recv()
            array_data = json.loads(data)

            # Print the received data for demonstration purposes
            print("Received data: ", array_data)

# Run the real-time client
asyncio.get_event_loop().run_until_complete(receive_array())
asyncio.get_event_loop().run_forever()

I am expecting to receive guidance on how to troubleshoot and resolve the "Failed to listen on tcp://0.0.0.0:8080: Address already in use" error when setting up a websocket server on my website I'm trying to make real-time send data between admin python and the python clients throw php server

0

There are 0 answers