Created
December 30, 2012 07:26
-
-
Save leepfrog/4411410 to your computer and use it in GitHub Desktop.
Android WebSockets plugin for Cordova 2.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WebSocket | |
| constructor: (url) -> | |
| cordova.exec (-> return true), (-> return false), "WebSocketPlugin", "createSocket", [url] | |
| cordova.exec @_onerror, (->), "WebSocketPlugin", "registerCallback", ["onerror"] | |
| cordova.exec @_onopen, (->), "WebSocketPlugin", "registerCallback", ["onopen"] | |
| cordova.exec @_onclose, (->), "WebSocketPlugin", "registerCallback", ["onclose"] | |
| cordova.exec @_onmessage, (->), "WebSocketPlugin", "registerCallback", ["onmessage"] | |
| send: (message) -> | |
| cordova.exec (-> return true), (-> return false), "WebSocketPlugin", "send", [message] | |
| close: -> | |
| cordova.exec (-> return undefined), (-> return undefined), "WebSocketPlugin", "close", [] | |
| _onerror: => | |
| @onerror | |
| _onopen: => | |
| @onopen | |
| _onclose: => | |
| @onclose | |
| _onmessage: (message) => | |
| @onmessage {data: message} | |
| onerror: -> | |
| onopen: -> | |
| onclose: -> | |
| onmessage: (event) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mochaleaf.cordova.plugin; | |
| import java.net.URISyntaxException; | |
| import java.util.HashMap; | |
| import org.apache.cordova.api.CallbackContext; | |
| import org.apache.cordova.api.CordovaPlugin; | |
| import org.apache.cordova.api.PluginResult; | |
| import org.json.JSONArray; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| import com.mochaleaf.cordova.plugin.WebSocket.CallbackType; | |
| /** | |
| * This class echoes a string called from JavaScript. | |
| */ | |
| public class WebSocketPlugin extends CordovaPlugin { | |
| WebSocket socket; | |
| HashMap <CallbackType, CallbackContext> contexts; | |
| @Override | |
| public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { | |
| if (action.equals("createSocket")) { | |
| String url = args.getString(0); | |
| try { | |
| this.createSocket(url, callbackContext); | |
| } catch (URISyntaxException e) { | |
| callbackContext.error("Invalid URL passed in to websocket."); | |
| return false; | |
| } | |
| return true; | |
| } else if (action.equals("send")) { | |
| String message = args.getString(0); | |
| this.send(message, callbackContext); | |
| return true; | |
| } else if (action.equals("close")) { | |
| this.close(callbackContext); | |
| return true; | |
| } else if (action.equals("registerCallback")) { | |
| String type = args.getString(0); | |
| this.registerCallback(type, callbackContext); | |
| return true; | |
| } | |
| return false; | |
| } | |
| // Triggered from our WebSocket subclass to send a particular callback | |
| public void triggerCallback(CallbackType callbackType, String... data) { | |
| CallbackContext context = this.contexts.get(callbackType); | |
| if (context != null) { | |
| if (data.length == 1) { | |
| context.success(data[0]); | |
| } else { | |
| context.success(); | |
| } | |
| } | |
| } | |
| // Creates a websocket to use | |
| private void createSocket(String url, CallbackContext callbackContext) throws URISyntaxException { | |
| this.socket = new WebSocket(url, this); | |
| this.contexts = new HashMap <CallbackType, CallbackContext>(); | |
| callbackContext.success(); | |
| } | |
| // Opens our socket if it exists | |
| private void send(String message, CallbackContext callbackContext) { | |
| if (null != socket) { | |
| this.socket.send(message); | |
| callbackContext.success(); | |
| } else { | |
| callbackContext.error("We do not have a socket open."); | |
| } | |
| } | |
| // Closes our socket | |
| private void close(CallbackContext callbackContext) { | |
| this.socket = null; | |
| this.contexts = null; | |
| callbackContext.success(); | |
| } | |
| // Registers a callback to use | |
| private void registerCallback(String type, CallbackContext callbackContext) { | |
| if (type.equals("onerror")) { | |
| this.contexts.put(CallbackType.ONERROR, callbackContext); | |
| } else if (type.equals("onopen")) { | |
| this.contexts.put(CallbackType.ONOPEN, callbackContext); | |
| } else if (type.equals("onclose")) { | |
| this.contexts.put(CallbackType.ONCLOSE, callbackContext); | |
| } else if (type.equals("onmessage")) { | |
| this.contexts.put(CallbackType.ONMESSAGE,callbackContext); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The issue is this:
12-29 23:28:19.050: W/CordovaPlugin(14810): Attempted to send a second callback for ID: WebSocketPlugin1559345860