Reputation: 109
I'm making a multiplayer game with node.js but it crashes when client sends invalid packet.
Server:
msg.readFloatLE(1, true);
Client:
msg.setUint8(1, 1);
So, if server is waiting for a float but client sends an int, it crashes with this error:
C:\Program Files\nodejs\node.exe: src\node_buffer.cc:752: Assertion `(offset + sizeof(T)) <= (ts_obj_length)' failed.
I'm using ws library but i'm pretty sure that it's about node.js. How can i fix this?
Upvotes: 0
Views: 252
Reputation: 314
If you take a look at the node documentation for readFloatLE it clearly says it throws exception for your example.
// Throws an exception: RangeError: Index out of range
console.log(buf.readFloatLE(1));
You can take a look at documentation here
The question is what exactly do you try to accomplish? and is readFloatLE right tool for that?
Upvotes: 0
Reputation: 3154
As @Ved pointed in his answer - try/catch would be the best approach.
Just to suggest something not standard:
const defaultCriticalValue = 0.0;
const floatNum = parseFloat('a') || defaultCriticalValue;
msg.readFloatLE(floatNum, true);
Upvotes: 0
Reputation: 12093
You can handle exception using try
catch
try {
msg.readFloatLE(1, true);
}
catch(e) {
console.log(e,"error)
}
Upvotes: 1