Created
January 8, 2019 23:06
-
-
Save nsjames/22716b22640ab00bdb800bd15221ba81 to your computer and use it in GitHub Desktop.
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.scatternative; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import org.java_websocket.WebSocket; | |
import org.java_websocket.drafts.Draft; | |
import org.java_websocket.exceptions.InvalidDataException; | |
import org.java_websocket.handshake.ClientHandshake; | |
import org.java_websocket.handshake.ServerHandshakeBuilder; | |
import org.java_websocket.server.WebSocketServer; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
import com.facebook.react.bridge.NativeModule; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
import com.facebook.react.bridge.Callback; | |
import com.facebook.react.modules.core.DeviceEventManagerModule; | |
import java.util.Map; | |
import java.util.HashMap; | |
class Socket extends WebSocketServer { | |
private HashMap<String, WebSocket> connections = new HashMap<String, WebSocket>(); | |
ReactContext context; | |
public Socket(ReactContext reactContext) { | |
super( new InetSocketAddress("127.0.0.1", 50005) ); | |
context = reactContext; | |
} | |
private void sendEvent(String eventName, String params) { | |
context | |
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | |
.emit(eventName, params); | |
} | |
public void reply(String id, String result){ | |
WebSocket conn = connections.get(id); | |
connections.remove(id); | |
JsonParser parser = new JsonParser(); | |
JsonObject jsonResult = parser.parse(result).getAsJsonObject(); | |
conn.send("42/scatter,[\"api\", "+jsonResult.toString()+"]"); | |
} | |
public void pairReply(String id, boolean result){ | |
WebSocket conn = connections.get("pair"); | |
connections.remove(id); | |
conn.send("42/scatter,[\"paired\","+result+"]"); | |
} | |
public void rekey(String id){ | |
WebSocket conn = connections.get(id); | |
conn.send("42/scatter,[\"rekey\"]"); | |
} | |
@Override | |
public void onOpen(WebSocket conn, ClientHandshake handshake ) { | |
sendEvent("socket", "open"); | |
conn.send("40"); | |
conn.send("40/scatter"); | |
conn.send("42/scatter,[\"connected\"]"); | |
} | |
@Override | |
public void onClose( WebSocket conn, int code, String reason, boolean remote ) { | |
sendEvent("socket","closed"); | |
} | |
@Override | |
public void onMessage( WebSocket conn, String message ) { | |
// Irrelevant Handshake / Upgrade | |
if(message.contains("40/scatter")) { | |
return; | |
} | |
String msg = message.replaceFirst("42/scatter,", ""); | |
JsonParser parser = new JsonParser(); | |
JsonArray typeAndData = parser.parse(msg).getAsJsonArray(); | |
String type = typeAndData.get(0).getAsString(); | |
JsonObject data = typeAndData.get(1).getAsJsonObject().get("data").getAsJsonObject(); | |
if(type.equals("pair")){ | |
connections.put("pair", conn); | |
} | |
else if (type.equals("api")){ | |
connections.put(data.get("id").getAsString(), conn); | |
} | |
sendEvent("api", msg); | |
} | |
@Override | |
public void onError( WebSocket conn, Exception ex ) { | |
//sendEvent("socket", ex.getMessage()); | |
sendEvent("socket","failed"); | |
ex.printStackTrace(); | |
} | |
@Override | |
public void onStart() { | |
sendEvent("socket", "started"); | |
} | |
} | |
public class SocketServer extends ReactContextBaseJavaModule { | |
private Socket server; | |
ReactApplicationContext context; | |
public SocketServer(ReactApplicationContext reactContext) { | |
super( reactContext ); | |
context = reactContext; | |
} | |
@Override | |
public String getName() { | |
return "SocketServer"; | |
} | |
private void sendEvent(String eventName, String params) { | |
context | |
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | |
.emit(eventName, params); | |
} | |
@ReactMethod | |
public void init() { | |
server = new Socket(context); | |
server.setReuseAddr(true); | |
} | |
@ReactMethod | |
public void start(){ | |
try { | |
server.start(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@ReactMethod | |
public void stop(){ | |
try { | |
server.stop(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@ReactMethod | |
public void reply(String id, String result){ | |
server.reply(id, result); | |
} | |
@ReactMethod | |
public void pairReply(String id, boolean result){ | |
server.pairReply(id, result); | |
} | |
@ReactMethod | |
public void rekey(String id){ | |
server.rekey(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment