Reputation: 1706
I have a java program which collects realtime sensor data from a COM port and I want to send it to javascript without using node.js.
The sensor data should be givin arround at least 2 times a second.
I tough about using a database to communicate but that's probably to slow a solution. Browser support can be limited to google chrome if needed. (osx,win,linux)
What would be your ideal solution?
EDIT : - the java software should run on the same platform as the webbased javascript client.
Essentially the data being send is GPS,.. data which is shown on a map.
Upvotes: 0
Views: 1662
Reputation: 268
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
//JAVA
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
if (!(engine instanceof Invocable)) {
System.out.println("Invoking methods is not supported.");
return;
}
Invocable inv = (Invocable) engine;
String scriptPath = "/Users/sheikhhussain/IdeaProjects/JavaHome/src/calculator.js";
engine.eval("load('" + scriptPath + "')");
Object calculator = engine.get("calculator");
int x = 3;
int y = 4;
Object addResult = inv.invokeMethod(calculator, "add", x, y);
Object subResult = inv.invokeMethod(calculator, "subtract", x, y);
Object mulResult = inv.invokeMethod(calculator, "multiply", x, y);
Object divResult = inv.invokeMethod(calculator, "divide", x, y);
System.out.println(addResult.toString());
System.out.println(subResult);
System.out.println(mulResult);
System.out.println(divResult);
}
}
// JAVASCRIPT
var calculator = new Object();
calculator.add = function (n1, n2) "jjj";
calculator.subtract = function (n1, n2) n1 - n2;
calculator.multiply = function (n1, n2) n1 * n2;
calculator.divide = function (n1, n2) n1 / n2;
Upvotes: 1
Reputation: 7625
There are a number of ways you can do this.
-A java to flash to JavaScript bridge to communicate via sockets. This is the most compatible way since most browsers support flash. There are a few opensource AS3 bridges however you need sockets.
-A java applet and from the applet invoke JavaScript Code.If you have permission to read the com port is the easiest way otherwise you may have to struggle with applet certifications. The Java Runtime must be installed on your machine.
-A websocket if you use Firefox 8.0+ or a recent versión of google chrome to communicate with your java app wia websockets. Is the most modern way to do it. It will not work with IE but you don't need any other app as bridge between javascript and java, you may need to struggle to find a java socket server example, create your own implementation or use a web containger/app server that supports WS:// or WSS://
Upvotes: 1
Reputation: 438
LiveConnect is what you're looking for. It allows you to define communication methods from JS to Java and viceversa. And it's a built-in technology in the JDK.
Upvotes: 2