Skip to content

Instantly share code, notes, and snippets.

@atsuya
Created December 6, 2011 07:42
Show Gist options
  • Save atsuya/1437232 to your computer and use it in GitHub Desktop.
Save atsuya/1437232 to your computer and use it in GitHub Desktop.
websocket binary data test - websocket.io
var socket = null;
$(function() {
socket = new WebSocket('ws://localhost:3000');
socket.binaryType = 'arraybuffer';
socket.onmessage = function(message) {
receiveBinary(message);
};
setTimeout(sendBinary, 1000);
});
function sendBinary() {
var byteArray = new Uint8Array(4);
byteArray[0] = 0x01;
byteArray[1] = 0x10;
byteArray[2] = 0xff;
byteArray[3] = 0xde;
socket.send(byteArray.buffer);
}
function receiveBinary(message) {
console.dir(message);
}
server.on('connection', function (client) {
console.log('connedted!');
client.on('message', function(data) {
console.log(Buffer.isBuffer(data));
console.log(data);
for (var i = 0; i < data.length; i++) {
console.log(data.readUInt8(i));
}
var buf = new Buffer(5);
buf.writeUInt8(0x3, 0);
buf.writeUInt8(0x4, 1);
buf.writeUInt8(0x23, 2);
buf.writeUInt8(0x42, 3);
buf.writeUInt8(0xff, 4)
// it fails and it seems like it tried to convert to text internally
client.send(buf);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment