socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.34k stars 975 forks source link

Connection handshake is not completed #666

Closed Sahara150 closed 2 years ago

Sahara150 commented 3 years ago

I have a websocket server with PHP, which works perfectly fine with pure JS. With the socket.io-client-java however it connects on server side, but the successful connection is never acknowledged by the client. Every timeout interval it just disconnects and connects again. I know that is not a problem of Ratchet, since there are no issues connecting via telnet or JS-console.

To Reproduce

Socket.IO server version: unknown Server

<?php

namespace agroSMS\Websockets;

use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;

class SocketConnection implements MessageComponentInterface
{
    protected \SplObjectStorage $clients;
    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    function onOpen(ConnectionInterface $conn)
    {
        $this->clients->attach($conn);
        error_log("New client attached");
    }

    function onClose(ConnectionInterface $conn)
    {
        $this->clients->detach($conn);
        error_log("Client detached");
    }

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

        $conn->close();
    }

    function onMessage(ConnectionInterface $from, $msg)
    {
        error_log("Received message: $msg");
        // TODO: Implement onMessage() method.
    }
}

Binary running in terminal:

<?php

use Ratchet\Server\IoServer;
use agroSMS\Websockets\SocketConnection;
use Ratchet\WebSocket\WsServer;
use Ratchet\Http\HttpServer;

require dirname(__DIR__) . '/vendor/autoload.php';

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new SocketConnection()
        )
    )
);

$server->run();

Command to execute it:

php socket-server.php

Socket.IO java client version: 2.0.1

Client

public class App {

    /**
     * Your application's main entry point.
     *
     * @param args Yet unused
     */
    public static void main( String[] args ) {
        System.out.println("Starting test");
        URI uri = URI.create("ws://<my-ip>:80");
        IO.Options options = IO.Options.builder()
                .setTimeout(60000)
                .setTransports(new String[]{WebSocket.NAME, Polling.NAME})
                .build();
        Socket socket = IO.socket(uri, options);
        socket.on(Socket.EVENT_CONNECT, objects -> System.out.println("Connection established"));
        socket.on(Socket.EVENT_CONNECT_ERROR, objects -> System.out.println(Arrays.toString(objects)));
        socket.connect();
    }
}

Expected behavior Terminal should show "Starting test" and after a short moment "Connection established". Client should attach to server.

Actual behavior Terminal shows: Starting test [io.socket.client.SocketIOException: timeout] [io.socket.client.SocketIOException: timeout] [io.socket.client.SocketIOException: timeout] [io.socket.client.SocketIOException: timeout] [io.socket.client.SocketIOException: timeout] [io.socket.client.SocketIOException: timeout] [io.socket.client.SocketIOException: timeout] [io.socket.client.SocketIOException: timeout] ...

Client attaches to server and after timeout detaches and attaches again.

Platform:

Additional context Client and server are run on same pc, there´s no wifi problems possible.

darrachequesne commented 3 years ago

The Socket.IO client is not a WebSocket implementation, so it will not be compatible with a plain WebSocket server. More information here: https://socket.io/docs/v4#What-Socket-IO-is-not

darrachequesne commented 2 years ago

Closed due to inactivity, please reopen if needed.