hey guys i was working on 1 project and now i got 1 problem or some of them. rlly need your hellp
| packet.js |
var zeroBuffer = new Buffer('00','hex');
module.exports = packet = {
//params: an array of javascrpt objects to be turned into buffers build: function(params){
var packetParts = {}; var packetSize = 0; params.forEach(function(param){ var buffer;
if(typeof param === 'string'){ buffer = new Buffer(param, 'utf8'); buffer = Buffer.concat([buffer, zeroBuffer], buffer.length + 1) } else if (typeof param === 'number'){ buffer = new Buffer(2); buffer.writeUInt16LE(param, 0); } else{ console.log("WARNING: Unknown data type in packet builder!"); }
packetSize += buffer.length; packetParts.push(buffer);
})
var dataBuffer = Buffer.concat(packetParts, packetSize);
var size = new Buffer(1); size.writeUInt8(dataBuffer.length + 1, 0);
var finalPacket = Buffer.concat([size, dataBuffer], size.length + dataBuffer.length)
return finalPacket;
} }
|
| client.js |
var now = require('performance-now'); var _ = require('underscore');
module.exports = function (){
//these objects will be added at runtime //this.socket = {} //this.socket = {}
this.initiate = function(){ var client = this;
//send the connection handshake packet to client. client.socket.write(packet.build(["HELLO",now().toString()]));
console.log('client initiated') }; this.data = function(
|
And then i start the game it should be shown :
client initiated
but i have got an error
c:\Users\...\packet.js:30
packetParts.push(buffer);
^ TypeError: undefined is not a function
at c:\Users\...\packet.js:30:25
at Array.forEach (native)
at Object.module.exports.packet.build (c:\Users\...\packet.js:14:16)
at initiate (c:\Users\...\client.js:17:36)
at Server.<anonymous> (c:\Users\....\server.js:37:16)
at Server.emit (events.js:107:17)
at TCP.onconnection (net.js:1303:8) Process finished with exit code 1
P.S. rlly srry for my terable english. rlly im waiting your hellp guys anyone can hellp me?
I'm not sure if it's the right place for this. Try StackOverflow next time (but be more careful about your english, use the built-in dictionary of your browser).
Anyway, you've defined packetParts as an object but you want an array. Just replace the {} with []:
var packetParts = {};To
This kind of mistake should not cause such panic though, be more careful about what you write...