public
Last active

websocket binary data test - socket.io

  • Download Gist
browser
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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);
});
});
server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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);
}

the both side receive a typeof string

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.