Android app throws exception with Tornado 3.1. Works with Tornado 2.1

579 Views Asked by At

I'm developing an Android app which communicates with a web server and needs push notifications from the server.

This app runs successfully and as expected with Tornado 2.1 as web server and using the weberknecht websockets in the Android client app.

However, with Tornado 3.1, the same app always returns an exception when I try calling the "websocket.connect()" method.

Exception in client app with Tornado 3.1 :-

connection failed: unknown status code 426 cause WebSocketException (id=830032153816)

I read on stackoverflow that weberknecht may not be compatible with the latest Tornado 3.1. Hence, I moved to Autobahn web socket. But if I import the autobahn websocket jar, the application crashes at the "connect" method. I tried jwebsocket client too. I'm facing the same issue with it too.

Am I missing something in my code ? Is weberknecht not compatible with Tornado 3.1 ? What are my alternatives for using Tornado 3.1 ? Or should I continue with Tornado 2.1 ?

My Application Code :-

public class MainActivity extends Activity  {

protected static final String TAG = null;
BaseTokenClient btc;
Toast testToast;
private  WebSocketConnection websocket; 
URI url;

private class AsyncThread extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            websocket.connect();
        } catch (de.roderick.weberknecht.WebSocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "";
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    url = null;
    try {
        url = new URI("ws://192.168.1.130:8888/ws");
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        websocket =  new WebSocketConnection(url);
    } catch (de.roderick.weberknecht.WebSocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Register Event Handlers
    websocket.setEventHandler(new WebSocketEventHandler() {
            public void onOpen()
            {
                    Log.e(TAG, "--open");
                    try {
                        websocket.send("HELLO");
                    } catch (de.roderick.weberknecht.WebSocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }

            public void onMessage(WebSocketMessage message)
            {
                    Log.e(TAG, "--received message: " + message.getText());
                    System.out.println(message.getText());
            }

            public void onClose()
            {
                    Log.e(TAG, "--close"); 
            }
    });

    new AsyncThread().execute();

}

}

My Tornado web server code :-

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hello World")

    def on_message(self, message):
        print 'message received %s' % message

    def on_close(self):
        print 'connection closed'

application = tornado.web.Application([
    (r'/ws', WSHandler),
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
1

There are 1 best solutions below

0
On

Try doing some debugging around here.
Examine the headers that are arriving at Tornado and that are leaving Weberknecht, here.