Click here to Skip to main content
15,885,757 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 357.2K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
/*!
 * ws: a node.js websocket client
 * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
 * MIT Licensed
 */

/**
 * Returns a Buffer from a "ff 00 ff"-type hex string.
 */

getBufferFromHexString = function(byteStr) {
  var bytes = byteStr.split(' ');
  var buf = new Buffer(bytes.length);
  for (var i = 0; i < bytes.length; ++i) {
    buf[i] = parseInt(bytes[i], 16);
  }
  return buf;
}

/**
 * Returns a hex string from a Buffer.
 */

getHexStringFromBuffer = function(data) {
  var s = '';
  for (var i = 0; i < data.length; ++i) {
    s += padl(data[i].toString(16), 2, '0') + ' ';
  }
  return s.trim();
}

/**
 * Splits a buffer in two parts.
 */

splitBuffer = function(buffer) {
  var b1 = new Buffer(Math.ceil(buffer.length / 2));
  buffer.copy(b1, 0, 0, b1.length);
  var b2 = new Buffer(Math.floor(buffer.length / 2));
  buffer.copy(b2, 0, b1.length, b1.length + b2.length);
  return [b1, b2];
}

/**
 * Performs hybi07+ type masking on a hex string or buffer.
 */

mask = function(buf, maskString) {
  if (typeof buf == 'string') buf = new Buffer(buf);
  var mask = getBufferFromHexString(maskString || '34 83 a8 68');
  for (var i = 0; i < buf.length; ++i) {
    buf[i] ^= mask[i % 4];    
  }
  return buf;
}

/**
 * Returns a hex string representing the length of a message
 */
 
getHybiLengthAsHexString = function(len, masked) {  
  if (len < 126) {
    var buf = new Buffer(1);
    buf[0] = (masked ? 0x80 : 0) | len;
  }
  else if (len < 65536) {
    var buf = new Buffer(3);
    buf[0] = (masked ? 0x80 : 0) | 126;
    getBufferFromHexString(pack(4, len)).copy(buf, 1);
  }
  else {
    var buf = new Buffer(9);
    buf[0] = (masked ? 0x80 : 0) | 127;
    getBufferFromHexString(pack(16, len)).copy(buf, 1);
  }
  return getHexStringFromBuffer(buf);
}

/**
 * Unpacks a Buffer into a number.
 */

unpack = function(buffer) {
  var n = 0;
  for (var i = 0; i < buffer.length; ++i) {
    n = (i == 0) ? buffer[i] : (n * 256) + buffer[i];
  }
  return n;
}

/**
 * Returns a hex string, representing a specific byte count 'length', from a number.
 */

pack = function(length, number) {
  return padl(number.toString(16), length, '0').replace(/([0-9a-f][0-9a-f])/gi, '$1 ').trim();
}

/**
 * Left pads the string 's' to a total length of 'n' with char 'c'.
 */

padl = function(s, n, c) { 
  return new Array(1 + n - s.length).join(c) + s;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions