Combining java and nodejs for android app
Asked Answered
E

3

7

I'm working on Android game which is turn based, and I've chosen Nodejs for the server side. I've been exploring for about two weeks how to communicate from the Android client side to the Nodejs server. Is there any way to communicate between the two.

Kindly help me if any one have any experience with such a project.

Eolian answered 17/9, 2012 at 5:50 Comment(1)
I don't really know anything about nodejs, but I assume that you don't really care specifically about Android, but really interfacing to your nodejs server using any sort of socket based infrastructure.. (I.e., nothing makes this Android specific rather than java specific..)Miyamoto
S
1

There are lots of options for something like this depending on what your game requires for communicating between client and server. For instance looking up "TCP clients for android" here shows up answers like this. If fast updates are important between server and client then UDP is one option, if your game can cope with the loss of some packets in the middle.

Besides TCP/UDP you also have things like WebSockets for Android.

Superstar answered 17/9, 2012 at 6:42 Comment(0)
U
0

Combining Android & nodejs is no problem. First you have to define a middelware. You can use REST-Webservices or any other technology for the communication between the node server and the android client. There are many standard APIs and protocols. I would use Websocket for the communication. You can find Android / Node.js APIs with WebSocket support here:

Unbalanced answered 17/9, 2012 at 8:50 Comment(0)
S
0

You can use Volley in Android to make a json POST or GET request.

And for the NODE JS you can use node's built-in http module to create a simple HTTP server and then receive data from the req object.

const http=require('http');
const stringDecoder=require('string_decoder').StringDecoder;

var httpServer=http.createServer(function(req,res){    
    unifinedServer(req,res);
});
//Just another method. 
var unifinedServer=function(req,res){

    var decoder=new stringDecoder('utf-8');
    var buffer='';
    //reading the post data. 
    req.on('data',function(data){
        buffer+=decoder.write(data);
    });
    //Reading of data is completed. 
    req.on('end',function(){
        buffer+=decoder.end(); 
    // Do what ever you want to do with the POST data. 
    });    
}
//The Server is listening on a specific port. 
httpServer.listen(7000,function(){
    console.log("Server is now listening on Port..."+7000);
});

For the Android Code you can do this with volley:

String url = "http://example.com";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
        (Request.Method.POST, url, postJsonObject, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        mTextView.setText("Response: " + response.toString());
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO: Handle error

    }
});

// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
Sadowski answered 14/10, 2018 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.