简体   繁体   中英

(Node.js, socket.io) JSONObject cannot be converted to int

I'm trying to create a multiplayer game, if the lobby isn't full the host has AI ships which emit their actions to a server where the server broadcasts their "moves" to a blank ship in the clients game. The broadcast is accompanied by an "i" which is the AIShips identifier so the correct ship is told to move in the clients game.

Here's the code in order that it's used:

Initial emit: (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();
}

My server: (index.js)

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

The response to the broadcast: (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();
                }
});

The error: W/System.err: org.json.JSONException: Value {"i":0} at i of type org.json.JSONObject cannot be converted to int W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)

It's because you are nesting the { "i": identifier } object. Take a look (explained in the commented code below):

Initial emit: (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);
...

My server: (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})
});

The response to the broadcast: (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!
....
});

Solution (not the only one, once you know the reason, tho):
In your index.js change the payload you are sending to something like this, for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM