有可能做到这一点吗?或者,我应该问的问题是,这是可行的吗?
还是只使用Java/Android前端和Java后端服务器会更好?
在多人游戏中使用node js后端实用吗?或者可能只适用于不需要太多资源的web应用程序
如果它( java -nodejs)适用于多人游戏,那么如何让java客户端能够与node js服务器进行通信呢?
发布于 2016-05-30 08:18:42
如果您正在构建API,则不必担心编写它所使用的语言,与其通信的方式将保持不变(UDP/HTTP/WS请求)。尽管如此,您正在创建一个多人游戏,因此您需要良好的后端性能。
NodeJS很好,非常好!Erlang更好;)
发布于 2018-10-15 01:50:04
是的,这是可行的。Node在处理请求方面非常高效和快速。因为它工作在基于事件的模型上,所以它对服务器端很好。如果你使用Android和NodeJs作为服务器端,我可以提供帮助:
您可以在Android中使用Volley发出json POST
或GET
请求。
对于节点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);
https://stackoverflow.com/questions/37516142
复制相似问题