Created
December 6, 2011 07:49
-
-
Save atsuya/1437249 to your computer and use it in GitHub Desktop.
websocket binary data test - websocket-node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
var buffer = new Uint8Array(message.data); | |
console.dir(buffer); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var connection = request.accept(null, request.origin); | |
console.log((new Date()) + " Connection accepted."); | |
connection.on('message', function(message) { | |
if (message.type === 'utf8') { | |
console.log("Received Message: " + message.utf8Data); | |
connection.sendUTF(message.utf8Data); | |
} | |
else if (message.type === 'binary') { | |
console.log("Received Binary Message of " + message.binaryData.length + " bytes"); | |
console.log(Buffer.isBuffer(message.binaryData)); | |
console.log(message.binaryData); | |
for (var i = 0; i < message.binaryData.byteLength; 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) | |
connection.sendBytes(buf); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment