Skip to content

Instantly share code, notes, and snippets.

@jcrugzz
Forked from max-mapper/Websocket.java
Created September 20, 2012 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcrugzz/3754210 to your computer and use it in GitHub Desktop.
Save jcrugzz/3754210 to your computer and use it in GitHub Desktop.
android websocket wss:// plugin (not done)
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);
}
};
}
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()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment