mrkd
7/30/2014 - 4:29 PM

binaryjs-client.html

$ node binaryjs-server.js 
Starting server on port 9000
100
connected to: [object Object]
Entry 0: 100
Entry 1: 101
Entry 2: 97
Entry 3: 100
Entry 4: 98
Entry 5: 101
Entry 6: 101
Entry 7: 102

/Users/markd/node_modules/binaryjs/node_modules/binarypack/lib/binarypack.js:187
        throw new Error('Type "' + constructor.toString() + '" not yet support
              ^
Error: Type "function ArrayBuffer() { [native code] }" not yet supported
    at BinaryPack.Packer.pack (/Users/markd/node_modules/binaryjs/node_modules/binarypack/lib/binarypack.js:187:15)
    at BinaryPack.Packer.pack_array (/Users/markd/node_modules/binaryjs/node_modules/binarypack/lib/binarypack.js:245:10)
    at BinaryPack.Packer.pack (/Users/markd/node_modules/binaryjs/node_modules/binarypack/lib/binarypack.js:177:14)
    at Object.BinaryPack.pack (/Users/markd/node_modules/binaryjs/node_modules/binarypack/lib/binarypack.js:11:25)
    at BinaryStream._write (/Users/markd/node_modules/binaryjs/lib/stream.js:75:22)
    at BinaryStream.write (/Users/markd/node_modules/binaryjs/lib/stream.js:81:20)
    at BinaryClient.send (/Users/markd/node_modules/binaryjs/lib/client.js:182:14)
    at BinaryServer.<anonymous> (/Users/markd/workingDirectory/webSockets/binaryjs-server.js:36:9)
    at BinaryServer.EventEmitter.emit (events.js:95:17)
    at WebSocketServer.<anonymous> (/Users/markd/node_modules/binaryjs/lib/server.js:36:10)

function strToU8(str) {
	var u8 = new Uint8Array(str.length);
	    str.split("").forEach(function(char,i){ 
        var code = char.charCodeAt(0); 
        u8[i] = code;
    })
	return u8.buffer;
}

var BinaryServer = require('binaryjs').BinaryServer;
//var BinaryServer = require('../../').BinaryServer;
var fs = require('fs');

// Start Binary.js server
var server = BinaryServer({
	port: 9000
});
console.log("Starting server on port 9000");

// message to send
var deadbeef = strToU8("deadbeef");

// Wait for new user connections
server.on('connection', function(client) {
	console.log("connected to: " + client.toString());
	var buffer = new ArrayBuffer(8);
	var int8View = new Int8Array(buffer);


	for (var i = 0; i < int8View.length; i++) {
		int8View[i] = deadbeef[i];
		console.log("Entry " + i + ": " + int8View[i]);
	}

	client.send(buffer);
	new Date().getTime();

	console.log("sent buffer: " + Date.now());
});
<html>
<head>
  <script src="http://cdn.binaryjs.com/0/binary.js"></script>
  <script>

  var hexChar = ["0", "1", "2", "3", "4", "5", "6", "7","8", "9", "A", "B", "C", "D", "E", "F"];
 
function byteToHex(b) {
  return hexChar[(b >> 4) & 0x0f] + hexChar[b & 0x0f];
}
    // Connect to Binary.js server
    var client = new BinaryClient('ws://localhost:9000');
    // Received new stream from server!
    client.on('stream', function(stream, meta){ 
      console.log('new stream');   
      // Buffer for parts
      var parts = [];
      // Got new data
      stream.on('data', function(data){
        parts.push(data);
        console.log("data: " + data);
      });
      stream.on('end', function(){
        // Display new data in browser!
        // var img = document.createElement("img");
        // img.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
        document.body.appendChild('<p>' + parts + '</p>');
        console.log("parts: " + parts);
      });
      stream.on('error', function() {
        console.log('Error');
      })
    });
  </script>
</head>
<body>
</body>
</html>