我正在开发基于turn的Android游戏,我选择了Nodejs作为服务器端。我已经花了大约两周的时间探索如何从Android客户端到Nodejs服务器进行通信。有没有办法在两者之间进行沟通。
请帮助我,如果任何人有任何这样的项目经验。
发布于 2012-09-17 14:42:50
对于这样的东西,有很多选择,这取决于您的游戏需要在客户端和服务器之间进行通信。例如,在这里搜索“安卓的TCP客户端”,就会得到类似于this的答案。如果服务器和客户端之间的快速更新很重要,那么UDP是一种选择,如果你的游戏可以处理中间的一些数据包的丢失。
除了TCP/UDP之外,你还可以使用WebSockets for Android。
发布于 2018-10-15 01:43:01
您可以在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/12453754
复制相似问题