Example usage for org.apache.cordova CallbackContext isFinished

List of usage examples for org.apache.cordova CallbackContext isFinished

Introduction

In this page you can find the example usage for org.apache.cordova CallbackContext isFinished.

Prototype

public boolean isFinished() 

Source Link

Usage

From source file:com.knowledgecode.cordova.websocket.ConnectionTask.java

License:Apache License

@Override
public void execute(JSONArray args, CallbackContext ctx) {
    try {//from  w  w w.j a va  2 s . c  o  m
        WebSocketClient client = _factory.newWebSocketClient();

        int id = args.getInt(0);
        URI uri = new URI(args.getString(1));
        String protocol = args.getString(2);
        JSONObject options = args.getJSONObject(5);
        String origin = options.optString("origin", args.getString(3));
        String agent = options.optString("agent", args.getString(4));
        long maxConnectTime = options.optLong("maxConnectTime", MAX_CONNECT_TIME);

        client.setMaxTextMessageSize(options.optInt("maxTextMessageSize", MAX_TEXT_MESSAGE_SIZE));
        client.setMaxBinaryMessageSize(options.optInt("maxBinaryMessageSize", MAX_BINARY_MESSAGE_SIZE));
        if (protocol.length() > 0) {
            client.setProtocol(protocol);
        }
        if (origin.length() > 0) {
            client.setOrigin(origin);
        }
        if (agent.length() > 0) {
            client.setAgent(agent);
        }
        setCookie(client.getCookies(), uri.getHost());

        WebSocketGenerator gen = new WebSocketGenerator(id, ctx);

        gen.setOnOpenListener(new OnOpenListener() {
            @Override
            public void onOpen(int id, Connection conn) {
                _map.put(id, conn);
            }
        });
        gen.setOnCloseListener(new OnCloseListener() {
            @Override
            public void onClose(int id) {
                if (_map.indexOfKey(id) >= 0) {
                    _map.remove(id);
                }
            }
        });
        client.open(uri, gen, maxConnectTime, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        if (!ctx.isFinished()) {
            PluginResult result = new PluginResult(Status.ERROR);
            result.setKeepCallback(true);
            ctx.sendPluginResult(result);
        }
    }
}