Created
September 7, 2012 21:42
-
-
Save max-mapper/3669939 to your computer and use it in GitHub Desktop.
android websocket wss:// plugin (not done)
This file contains 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.phonegap.plugins.websocket; | |
import android.os.Bundle; | |
import android.util.Log; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.apache.cordova.api.Plugin; | |
import org.apache.cordova.api.PluginResult; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import org.java_websocket.client.WebSocketClient; | |
import org.java_websocket.drafts.Draft; | |
import org.java_websocket.drafts.Draft_76; | |
import org.java_websocket.handshake.ServerHandshake; | |
public class Websocket extends Plugin { | |
protected static final String LOG_TAG = "Cordova Websocket"; | |
private String callbackId = null; | |
public PluginResult execute(String action, JSONArray args, String callback) { | |
callbackId = callback; | |
if (action.equalsIgnoreCase("connect")) { | |
try { | |
String wsurl = args.getString(0); | |
connect(wsurl); | |
} catch (JSONException e) { | |
Log.e(LOG_TAG, e.getMessage(), e); | |
} | |
} | |
PluginResult result = null; | |
result = new PluginResult(PluginResult.Status.NO_RESULT); | |
result.setKeepCallback(true); | |
return result; | |
} | |
public void connect(String wsurl) { | |
try { | |
WebSocketClient c = new WebSocketClient( new URI( wsurl ), new Draft_76() ) { | |
@Override | |
public void onMessage( String message ) { | |
try { | |
JSONObject obj = new JSONObject(); | |
obj.put("message", message); | |
PluginResult res = new PluginResult(PluginResult.Status.OK, obj); | |
res.setKeepCallback(true); | |
success(res, callbackId); | |
} catch (JSONException e) { | |
Log.e(LOG_TAG, e.getMessage(), e); | |
} | |
} | |
@Override | |
public void onOpen( ServerHandshake handshake ) { | |
try { | |
JSONObject obj = new JSONObject(); | |
obj.put("open", "true"); | |
PluginResult res = new PluginResult(PluginResult.Status.OK, obj); | |
res.setKeepCallback(true); | |
success(res, callbackId); | |
} catch (JSONException e) { | |
Log.e(LOG_TAG, e.getMessage(), e); | |
} | |
} | |
@Override | |
public void onClose( int code, String reason, boolean remote ) { | |
try { | |
JSONObject obj = new JSONObject(); | |
obj.put("error", reason); | |
PluginResult res = new PluginResult(PluginResult.Status.OK, obj); | |
res.setKeepCallback(true); | |
success(res, callbackId); | |
} catch (JSONException e) { | |
Log.e(LOG_TAG, e.getMessage(), e); | |
} | |
} | |
@Override | |
public void onError( Exception ex ) { | |
try { | |
JSONObject obj = new JSONObject(); | |
obj.put("error", ex); | |
PluginResult res = new PluginResult(PluginResult.Status.OK, obj); | |
res.setKeepCallback(true); | |
success(res, callbackId); | |
} catch (JSONException e) { | |
Log.e(LOG_TAG, e.getMessage(), e); | |
} | |
} | |
}; | |
} catch ( URISyntaxException ex ) { | |
Log.e(LOG_TAG, ex.getMessage(), ex); | |
} | |
}; | |
} |
This file contains 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
var Websocket = function() { | |
this.messageCallback = false | |
} | |
Websocket.prototype.connect = function(server, cb) { | |
cordova.exec(cb, cb, "Websocket", "connect", [server]) | |
} | |
Websocket.prototype.send = function(message) { | |
cordova.exec(null, null, "Websocket", "send", [{message: message}]) | |
} | |
Websocket.prototype.onMessage = function(cb) { | |
this.messageCallback = cb | |
} | |
Websocket.prototype.messageReceiver = function(message) { | |
if (this.messageCallback) this.messageCallback(message) | |
} | |
cordova.addConstructor(function() { | |
if (!window.plugins) window.plugins = {} | |
window.plugins.websocket = new Websocket() | |
}) |
Hey man I'd like to help out if possible to get this in there as a plugin. You have a current status? I'm not too proficient in java myself but had https://gist.github.com/3754275 working in cordova 1.8 as a workaround and was able to connect via socket.io. I know there was an issue with it working because the html is loaded with a file:// but I am not sure if any of that has changed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man I'd like to help out if possible to get this in there as a plugin. You have a current status? I'm not too proficient in java myself but had https://gist.github.com/3754275 working in cordova 1.8 as a workaround and was able to connect via socket.io. I know there was an issue with it working because the html is loaded with a file:// but I am not sure if any of that has changed.