可以这样做吗?或者,也许,我应该问的问题是,可行吗?
或者坚持使用Java/Android前端和Java后端服务器会更好吗?
节点 js 后端是否适用于多人游戏?或者也许只是对于不需要太多的 Web 应用程序
如果它(java-nodejs)对于多人游戏是实用的,那么如何让java客户端能够与节点js服务器进行通信?
如果你正在构建一个API,你不必担心你编写它的语言,与它通信的方式将保持不变(UDP/HTTP/WS请求)。尽管如此,您正在创建一个多人游戏,因此您需要后端的良好性能。
NodeJS很好,非常好!Erlang 是更好的;)
是的,这是可行的。Node在处理请求方面非常高效和快速。由于它适用于基于事件的模型,因此非常适合服务器端。如果您正在使用Android和NodeJs作为服务器端,我可以提供帮助:
您可以在 Android 中使用 Volley 发出 json POST
或GET
请求。
对于 NODE JS,您可以使用 node 的内置http
模块来创建一个简单的 HTTP 服务器,然后从 req 对象接收数据。
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 代码,您可以使用凌空抽射执行此操作:
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);