0

I'm implement Autobahn to connnect to a server through WebSockets. When I hit connect, it opens the socket correctly and logs that socket is opened. I then try to send a request to the server which is simply {"request":"getSoftwareVersion"} , when the server receives this, it should send back the software version in a JSON object, the trouble is, that onMessage is never hit. Here is my code:

public class AutoBahnConnectRequest extends Request{
    private static WebSocketConnection mAutoBahnConnection;
    private String mSocketHostAddress;
    private final static String m_TAG = AutoBahnConnectRequest.class.getSimpleName();

    public AutoBahnConnectRequest(String SocketHostAddress){
        this.mAutoBahnConnection = new WebSocketConnection();
        this.mSocketHostAddress = SocketHostAddress;
    }

    @Override
    protected Void doInBackground(Void... params){
        try {
            mAutoBahnConnection.connect(mSocketHostAddress, new WebSocketHandler(){

                @Override
                public void onOpen() {
                    String requestSoftware = "{\"request\":\"getSoftwareVersion\"}";
                    Log.i(m_TAG, requestSoftware);
                    Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
                    mAutoBahnConnection.sendTextMessage(requestSoftware);
                }

                @Override
                public void onTextMessage(String payload) {
                    Log.i(m_TAG, "Got echo: " + payload);
                }

                @Override
                public void onClose(int code, String reason) {
                    Log.i(m_TAG, "Connection lost."+ reason);
                }
            });
        } catch (WebSocketException e) {
            Log.d(m_TAG, e.toString());
        }
        return null;
    }
}

This has been implemented with a html client like so (not with autobahn):

function getSoftwareVersion() {
    socket_di.send('{"request":"getSoftwareVersion"}');
}

and the onMessage receives the data. Can someone please tell me if I'm doing something wrong here?

Thank you.

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98

1 Answers1

0

I have figured out my issue regarding this. The WebSocket connection required a protocol and options to be added. So I changed this:

mAutoBahnConnection.connect(mSocketHostAddress, new WebSocketHandler(){

                @Override
                public void onOpen() {
                    String requestSoftware = "{\"request\":\"getSoftwareVersion\"}";
                    Log.i(m_TAG, requestSoftware);
                    Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
                    mAutoBahnConnection.sendTextMessage(requestSoftware);
                }

                @Override
                public void onTextMessage(String payload) {
                    Log.i(m_TAG, "Got echo: " + payload);
                }

                @Override
                public void onClose(int code, String reason) {
                    Log.i(m_TAG, "Connection lost."+ reason);
                }
            });
        } catch (WebSocketException e) {
            Log.d(m_TAG, e.toString());
        }

to this:

 mAutoBahnConnection.connect(mSocketHostAddress,new String[]{"this is my protocol"} ,new WebSocketHandler(){

                @Override
                public void onOpen() {
                    String requestSoftware = "{\"request\":\"getSoftwareVersion\"}";
                    Log.i(m_TAG, requestSoftware);
                    Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
                    mAutoBahnConnection.sendTextMessage(requestSoftware);
                }

                @Override
                public void onTextMessage(String payload) {
                    Log.i(m_TAG, "Got echo: " + payload);
                }

                @Override
                public void onRawTextMessage(byte[] payload) {
                    try {
                        rawText = new String(payload, "UTF-8");
                        Log.i(m_TAG, "ON RAW TEXT");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onBinaryMessage(byte[] payload) {
                    Log.i(m_TAG, "ON BINARY MESSAGE");
                }


                @Override
                public void onClose(int code, String reason) {
                    Log.i(m_TAG, "Connection lost."+ reason);
                }
            }, options);
        } catch (WebSocketException e) {
            Log.d(m_TAG, e.toString());
        }
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98