Framework for Java/JavaScript communication

Hi @ all.

Very long explanation for what I need and what I did so far

I want to create an agent based RTS or FPS game.
I have to use Java for case-based reasoning with myCBR.
Since I have to use Java, it makes sense to use Jade as agent programming tool.

The problem for me is sending parameters from Java to JavaScript and vice versa.
The agents has to move with units, so they need a connection from Java to JavaScript.
When units are hit, they lose life and so I have to transfer a life parameter from javaScript to Java, for agents to calculate new strategies.
Well, I think sending parameters to JavaScript is easier than the other way round.

I already tried rhino engine, but it canā€™t handle .html files, only .js files.
I also looked at JSweet, but I had trouble in installing it.
Applets and JSon are another tools that didnā€™t worked for me, but I didnā€™t really try JSon right, so Iā€™m planning to try it again.
Another tool I want to test is Spring. I was told it could work.

Also I want to try Ajax, since I need the communication between Java and JavaScript without refreshing the browser. Maybe something like document.getElementById(ā€˜idā€™).submit() will work to send paramters to Java.

My prefered IDE is Eclipse.

tl;dr

I need a framework for sending parameters from Java to JavaScript and vice versa (in Eclipse)
I tried: Rhino Engine, JSweet, JSon, Applet-Tag

Does someone has experience with this kind of stuff?
Maybe I did something wrong and one of these named tools will work.

Iā€™m grateful for every advice to make my task easier.
(And Iā€™m sorry for never being brief.)

.
.
.

Unimportant stuff about my previous work

Is there any interest in the previous development for a FPS with a playable character?
I donā€™t know if I will continue to work on it and maybe someone else want to do it.
Itā€™s amateurish and incomplete. Actually, only what I have shown so far in playgrounds but with a different environment.
If so, is there a special section where I should upload it?

Dropbox - FPS TRY.zip

  • WASD - Moving
  • Shift - ā€˜sprintā€™
  • V - switch camera
    (the first few seconds the camera focus is somwhere in the scene)

ajax, rpc, websocketsā€¦ you will probably need to recon and deep-dive each category with poc (proof of concept). When working with maps (GIS) we found rpc to be a big winner - because it is lightweight. But it depends on your context. Make sure to (prove) the framework does exactly what you need - before building it out. :slight_smile: We use node for the JS to JS benefit and for lightweight flex-abilities.

1 Like

The problem is probably that even I donā€™t really know what I want or how I do it :smiley:

For now I think I go with ajax/jquery and json/gson, because I only want data transfer between java (server) and javascript (client).

But even with that, I couldnā€™t figure out, how to pass objects not coming from a formular.
For example when I want to send a mesh as json string, how can I access it in java?

var serializedMesh = BABYLON.SceneSerializer.SerializeMesh(sphere);
var content = JSON.stringify(serializedMesh);

function sendData() {
    $.ajax({
        url: 'TestServlet',
        type: 'POST',
        dataType: 'json',
        data: content,
        success: function(data) {
            alert("done");
        }
    });
}

Maybe there is a similar method like request.getParameter(), like with formulars.

In the other direction, to get an object from the server, is quiet easy

client:

var getObject = fromJava();

function fromJava() {
    var result;
    $.ajax({
        async: false,
        url: 'TestServlet',
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            result = data.obj;
        }
    });
    return result;
}

Server:

JSONObject obj = new JSONObject();
obj.put("name", "foo");
obj.put("num", 19);
	
Map <String, Object> map = new HashMap<String, Object>();
map.put("obj", obj.toString());
write(response, map);

private void write(HttpServletResponse response, Map<String, Object> map) throws IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(map));
}

Is there someone with ideas?
I hope this fits in this topicā€¦
Or more in this forum, because it is more an ajax/json question than babylon.

1 Like

This really isnt exactly ā€œbabylon JSā€ thing.

For example I am using Java Tomcat server with websockets and it is working nicely.

On client side I do

websocket.send(JSON.stringify(messageWrapper));

and on server side it also simple

@OnMessage
public synchronized void onMessage(Session session, String msg) {
org.json.JSONObject obj = new JSONObject(msg);
}

This will parse java object from JSON data - I just need to retrieve actual attributes from that object with obj.getInt(ā€œparamNameā€);

It is really quite simple and straightforward once you dive into.

2 Likes

It is indeed quiet easyā€¦itā€™s surprising me. Although my example donā€™t work, because the string is too big.

 var content = JSON.stringify(BABYLON.SceneSerializer.SerializeMesh(sphere));
 websocket.send(content); 
 //results in an error when converting on server side

Iā€™m wondering why while googling all kinds of ā€˜java-javascript connectionā€™ the results never spoke of websocketsā€¦
Also Iā€™m surprised, that I donā€™t need extern tools/libraries (except json and gson), to implement all these.
Ajax can be refered with scripts like babylon and websockets are also part of tomcat.

(Iā€™m aware that you guyā€™s know this, Iā€™m just describing it for other people, who see this thread and not know this or hesitate to try like me)

The real hard part now is, to decide which reply I mark as ā€˜solved the problemā€™ā€¦
I think I just flip a coin :slight_smile:

1 Like

Websocket is better suited for real-time connection, maybe it is not the best solution to transfer large chunk of data. I am not aware about limits but I can imagine that there are some (maybe configurable on the server side).

What might help you is some sort of compresion ? It is always better not to waste bandwidth - I am usgng this one and it works well GitHub - nodeca/pako: high speed zlib port to javascript, works in browser & node.js

I am using it to transfer large bitmap from server to client in GZIPed form but it could be used in opposite diraction as well. I would GZIP that JSON data and then send via common AJAX probably in your situation.

There might be some info about Java Tomcat HTTP limits and how to change them java - HttpRequest maximum allowable size in tomcat? - Stack Overflow

1 Like