Skip to content

Instantly share code, notes, and snippets.

@gaboelnuevo
Last active September 24, 2019 07:28
Show Gist options
  • Save gaboelnuevo/42eca1d3cc1f500ac5c69a9b39de1e9a to your computer and use it in GitHub Desktop.
Save gaboelnuevo/42eca1d3cc1f500ac5c69a9b39de1e9a to your computer and use it in GitHub Desktop.
rn-vorlon-wrapper
import { Platform, YellowBox } from "react-native";
import _ from "lodash";
const ignoredYellowBox = console.ignoredYellowBox || [];
console.ignoredYellowBox = ["Setting a timer"].concat(...ignoredYellowBox);
YellowBox.ignoreWarnings(console.ignoredYellowBox);
var urljoin = require("url-join");
const io = require("socket.io-client");
const _connect = io.connect;
io.connect = (serverUrl, options = {}) =>
_connect(serverUrl, {
...options,
transports: ['websocket'],
});
const _require = id => {
if (id == "socket.io-client") {
return io;
}
if (id == "module") {
return {
_cache: []
};
}
};
const process = {
title: "RN",
platform: Platform.OS,
version: "7.1.0",
umask: () => 18,
cwd: () => "",
nextTick: setImmediate,
env: {
HOME: "/",
USERNAME: "[email protected]"
},
stdin: {
resume: () => {}
},
on: () => {},
argv: ["react-native"],
versions: {},
memoryUsage: () => {
return 0;
}
};
const runInScope = script => {
new Function(
"exports",
"require",
"module",
"window",
"global",
"process",
script
).bind(window)(exports, _require, module, undefined, global, process);
};
(function() {
exports.start = function(vorlonjsURL, dashboardId, callback) {
if (dashboardId == undefined) {
dashboardId = "default";
}
// var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
var vorlonNodeUrl = urljoin(
vorlonjsURL,
"vorlon.node.max.js/" + dashboardId
);
xhr.onload = async function() {
try {
// const scriptFile = FileSystem.cacheDirectory + "vorlon.js";
// await FileSystem.writeAsStringAsync(scriptFile, xhr.responseText);
await runInScope(xhr.responseText.concat("\nglobal.VORLON = VORLON;"));
if (window.VORLON && window.VORLON.Tools) {
// window.VORLON.Tools.CreateGUID = () => "<device uniqueid>";
}
window.VORLON.Core.StartClientSide(vorlonjsURL, dashboardId);
if (callback) {
console.log(vorlonNodeUrl);
callback(true, "n/a");
}
} catch (e) {
console.error(e);
console.log("Wrapper Vorlon.js error : " + e.message);
if (callback) {
callback(false, e.message);
}
}
};
xhr.open("get", vorlonNodeUrl);
xhr.send();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment