Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java (Node.js,socket.io)JSONObject无法转换为int_Java_Node.js_Json_Socket.io - Fatal编程技术网

Java (Node.js,socket.io)JSONObject无法转换为int

Java (Node.js,socket.io)JSONObject无法转换为int,java,node.js,json,socket.io,Java,Node.js,Json,Socket.io,我正在尝试创建一个多人游戏,如果大厅里没有人,主机上的AI飞船会将他们的动作发送到服务器,服务器会在客户端游戏中将他们的“动作”广播到空白飞船上。广播伴随着一个“i”,它是AIShips标识符,因此正确的船被告知在客户端游戏中移动 以下是代码的使用顺序: 初始发射:(AIShip.java) JSONObject data=newjsonobject(); 试一试{ data.put(“i”,identifier);//identifier是构造函数中的一个int集 gameScreen.spa

我正在尝试创建一个多人游戏,如果大厅里没有人,主机上的AI飞船会将他们的动作发送到服务器,服务器会在客户端游戏中将他们的“动作”广播到空白飞船上。广播伴随着一个“i”,它是AIShips标识符,因此正确的船被告知在客户端游戏中移动

以下是代码的使用顺序:

初始发射:(AIShip.java)

JSONObject data=newjsonobject();
试一试{
data.put(“i”,identifier);//identifier是构造函数中的一个int集
gameScreen.spaceSoccer.socket.emit(“moveAIShipForward”,数据);
}捕获(JSONException e){
e、 printStackTrace();
}
我的服务器:(index.js)

socket.on(“moveAIShipForward”,函数(i){
emit(“moveaishipfroward”,{id:socket.id,i:i})
});
对广播的回应:(SpaceSoccer.java)

.on(“moveAIShipForward”,新发射器.Listener(){
@凌驾
公共无效调用(对象…参数){
JSONObject数据=(JSONObject)参数[0];
试一试{
int i=data.getInt(“i”);//错误
gameScreen.AIShip[i].moveBodyForward();
}捕获(JSONException e){
e、 printStackTrace();
}
});
无法将类型为org.json.JSONObject的i处的错误W/System.err:org.json.JSONException:Value{“i”:0}转换为int
W/System.err:org.json.json.typeMismatch(json.java:100)

这是因为您嵌套了
{“i”:identifier}
对象

初始发射:(AIShip.java)

我的服务器:(index.js)

对广播的回应:(SpaceSoccer.java)

解决方案(不是唯一的一个,只要你知道原因,tho):
index.js
中,将要发送的有效负载更改为以下内容,例如:

socket.on("moveAIShipForward", function(data) {
    socket.broadcast.emit("moveAIShipForward", {id: socket.id, i: data.i})
});

您是否已登录
args[0]
?此参数包含什么内容?非常感谢您提供的详细答案!这是我第一次尝试使用服务器,我以前从未使用过js,因此非常感谢您的回答!
// here, the `i` is what you have sent earlier: `{ "i": 0 }`
socket.on("moveAIShipForward", function(i) {
    // here, you are passing the `{ "i": 0 }` object to the new object, under `i` key, the resulting object looks like this:
    // `{ id: 0, i: { i: 0 } }`
    // and it's emitted
    socket.broadcast.emit("moveAIShipForward", {id: socket.id, i: i})
});
.on("moveAIShipForward", new Emitter.Listener() {
...
            JSONObject data = (JSONObject) args[0];
            try {
                // now here, the object looks like above (in the .js file), so `.get("i")` would return a `{ "i": 0 }` obejct, which obviously cannot be converted to int.
                int i = data.getInt("i"); // { "i": 0 } is not an integer!
....
});
socket.on("moveAIShipForward", function(data) {
    socket.broadcast.emit("moveAIShipForward", {id: socket.id, i: data.i})
});