首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将java和nodejs结合用于android应用

将java和nodejs结合用于android应用
EN

Stack Overflow用户
提问于 2012-09-17 13:50:23
回答 2查看 8.8K关注 0票数 7

我正在开发基于turn的Android游戏,我选择了Nodejs作为服务器端。我已经花了大约两周的时间探索如何从Android客户端到Nodejs服务器进行通信。有没有办法在两者之间进行沟通。

请帮助我,如果任何人有任何这样的项目经验。

EN

回答 2

Stack Overflow用户

发布于 2012-09-17 14:42:50

对于这样的东西,有很多选择,这取决于您的游戏需要在客户端和服务器之间进行通信。例如,在这里搜索“安卓的TCP客户端”,就会得到类似于this的答案。如果服务器和客户端之间的快速更新很重要,那么UDP是一种选择,如果你的游戏可以处理中间的一些数据包的丢失。

除了TCP/UDP之外,你还可以使用WebSockets for Android。

票数 1
EN

Stack Overflow用户

发布于 2018-10-15 01:43:01

您可以在Android中使用Volley发起json POSTGET请求。

对于节点JS,您可以使用节点的内置http模块创建一个简单的HTTP服务器,然后从请求对象接收数据。

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);
});

对于Android代码,您可以使用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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12453754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档