java - (Node.js、socket.io)JSONObject 无法转换为 int

标签 java node.js json socket.io

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

这是使用它的代码:

初始发出:(AIShip.java)

JSONObject data = new JSONObject();
try {
    data.put("i", identifier); //Identifier is an int set once in the constructor
    gameScreen.spaceSoccer.socket.emit("moveAIShipForward", data);
} catch (JSONException e){
    e.printStackTrace();
}

我的服务器:(index.js)

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

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

.on("moveAIShipForward", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                JSONObject data = (JSONObject) args[0];
                try {
                    int i = data.getInt("i"); //error
                    gameScreen.AIShip[i].moveBodyForward();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
});

错误:W/System.err: org.json.JSONException: org.json.JSONObject 类型的 i 处的值 {"i":0} 无法转换为 int W/System.err:位于 org.json.JSON.typeMismatch(JSON.java:100)

最佳答案

这是因为您嵌套了 { "i":identifier } 对象。看一下(在下面的注释代码中进行了解释):

初始发出:(AIShip.java)

JSONObject data = new JSONObject();
...
    data.put("i", identifier);

    // here the `data` is like `{ "i": 0 }` and it's emmited
    gameScreen.spaceSoccer.socket.emit("moveAIShipForward", data);
...

我的服务器:(index.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})
});

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

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

解决方案(不是唯一的解决方案,一旦您知道原因):
在您的 index.js 中,将您要发送的有效负载更改为如下所示,例如:

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

关于java - (Node.js、socket.io)JSONObject 无法转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58731708/

相关文章:

javascript - MEAN堆栈中的动态html表单生成

使用复杂和嵌套数据在 Swift 4 中解析 JSON

javascript - JavaScript 中高效的排序数据结构

java - 将业务逻辑添加到 spring-data-rest 应用程序

node.js - 预建 Electron 模块分布

java - 如何在运行时确定泛型对象的声明类型?

node.js - ETXTBSY : text file is busy when running npm install (via Vagrant with Unix - Desktop OS is Windows)

javascript - Codeigniter 中自动完成多个值

java - 在 NetBeans GUI 生成器中使用 CardLayout

java - 以 & 作为分隔符的正则表达式