Skip to content

Instantly share code, notes, and snippets.

@atsuya
Created December 6, 2011 07:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atsuya/1437195 to your computer and use it in GitHub Desktop.
Save atsuya/1437195 to your computer and use it in GitHub Desktop.
websocket binary data test - socket.io
io.sockets.on('connection', function (socket) {
socket.on('message', function(data) {
console.log(Buffer.isBuffer(data));
console.log(data);
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)
socket.send(buf);
});
});
var socket = null;
$(function() {
socket = io.connect();
socket.on('message', function(data) {
receiveBinary(data);
});
setTimeout(sendBinary, 1000);
});
function sendBinary() {
var byteArray = new Uint8Array(4);
byteArray[0] = 0x31;
byteArray[1] = 0x10;
byteArray[2] = 0xff;
byteArray[3] = 0x20;
socket.send(byteArray.buffer);
}
function receiveBinary(data) {
console.dir(data);
}
@govo
Copy link

govo commented Jan 28, 2013

the both side receive a typeof string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment