Skip to content

Received Data From Client

徐昊 edited this page Nov 12, 2018 · 3 revisions

Register IO callback

     mServerManager = OkSocket.server(/* the port you need listened */).registerReceiver(new ServerActionAdapter() {
            @Override
            public void onClientConnected(IClient client, int serverPort, IClientPool clientPool) {
                //You need add a callback when client connected.
                //All IO things will through by this callback.
                //You'd better use one callback object to do that and you'll distinguish each client in this callback.
                client.addIOCallback(/* The implementation of IClientIOCallback */);
            }

            @Override
            public void onClientDisconnected(IClient client, int serverPort, IClientPool clientPool) {
                //You need remove the callback when client is disconnected.
                client.removeIOCallback(/* The callback object which you added */);
            }

        });

In IO callback

    @Override
    public void onClientRead(OriginalData originalData, IClient client, IClientPool<IClient, String> clientPool) {
        //The parameter originalData is the data from the server.
        //The parameter client is mean which one received this originalData
    }

    @Override
    public void onClientWrite(ISendable sendable, IClient client, IClientPool<IClient, String> clientPool) {
        //The parameter sendable is the data already sent.
        //The parameter client is mean which one sends this data.
    }
  • OriginalData
    • mHeadBytes the header you or the server definition
    • mBodyBytes the payload you got from server