Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Handle Socket

Egor Egorov edited this page Jan 5, 2018 · 2 revisions

ClusterWS Java Client provides an easy interface to communicate with ClusterWS server.

Please note that send and on events are used only for server <-> client communication, you won't be able to communicate with another user across different CPU and Machines. To communicate with another user across CPU and Machines use Pub/Sub system. (check client <-> client communication guide for more information)

Listen on events from server

To listen on events which are send from server you should use on method:

socket.on("my-event-name", new IEmitterListener() {
    @Override
    public void onDataReceived(Object data) {
       // your code to execute on event       
    }
});

ClusterWS Java Client reserved events:

socket.setClusterWSListener(new IClusterWSListener() {
    @Override
    public void onConnected() {
        // executed when client is connected to the server
        // your code to execute on event
    }

    @Override
    public void onError(Exception exception) {
        // executed when some error has happened
        // your code to execute on event
    }

    @Override
    public void onDisconnected(int code, String reason) {
        // executed when client is disconnected from the server
        // your code to execute on event
    }
});

Send events to the server

To send message to the server you should use send method:

socket.send("my-event-name", "any-data-you-want");

Try to do not send events which starts with # or reserved disconnect, connection, error events.