1

I am trying to create library for websocket send.

public class SocketCls {
    private WebSocketClient mWebSocketClient;
    private void connectWebSocket(String url) throws URISyntaxException {
        URI uri;
        uri = new URI(url);

        mWebSocketClient = new WebSocketClient(uri) {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                Log.i("Websocket", "Opened");
                mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
            }

            @Override
            public void onMessage(String s) {
                final String message = s;
              // Here i want to use callback
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                Log.i("Websocket", "Closed " + s);
            }

            @Override
            public void onError(Exception e) {
                Log.i("Websocket", "Error " + e.getMessage());
            }
        };
        mWebSocketClient.connect();
    }
}

Normally i can achieve it but when i want it to put in separate package how to use callback ? please can anyone help me out this

1 Answer 1

2

You can add some interface WebSocketListener, which will contain onMessage, onClose, onError, etc.

and in constructor of your SocketCls you can set this listener and use it in websockets callback

public class SocketCls {
public interface SocketListener  {
    void onOpen(ServerHandshake serverHandshake);
    void onMessage(String s);
    void onClose(int i, String s, boolean b);

}

SocketListener listener;

public SocketCls(SocketListener l) {
    listener = l;
}
private WebSocketClient mWebSocketClient;
private void connectWebSocket(String url) throws URISyntaxException {
    URI uri;
    uri = new URI(url);

    mWebSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.i("Websocket", "Opened");
            mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
            listener.onOpen(serverHandshake);
        }

        @Override
        public void onMessage(String s) {
            final String message = s;
            // Here i want to use callback
            listener.onMessage(s);
        }

        @Override
        public void onClose(int i, String s, boolean b) {
            Log.i("Websocket", "Closed " + s);
            listener.onClose(i, s, b);
        }

        @Override
        public void onError(Exception e) {
            Log.i("Websocket", "Error " + e.getMessage());
        }
    };
    mWebSocketClient.connect();
}

}

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain me how to call connectWebSocket method in another package with listener.. please
if you want to call connectWebSocket, you should make it public

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.