Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cantremember
Created April 30, 2017 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cantremember/955be8759e789d1e1565b69c438d925c to your computer and use it in GitHub Desktop.
Save cantremember/955be8759e789d1e1565b69c438d925c to your computer and use it in GitHub Desktop.
Socket.io Client messaging using Binary Mode
/**
* Socket.io => Client => Listener
*/
function onSocketMessage: (channel_id, array_buffer) =>
// ArrayBuffer => String
// http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
json = String.fromCharCode.apply(null, new Uint8Array(array_buffer));
try {
// explicitly decode the String as UTF-8 for Unicode
// https://github.com/mathiasbynens/utf8.js
json = utf8.decode(json)
data = JSON.parse(json)
}
catch (err) {
// ...
}
this.listeners.forEach(function(listener) {
listener.emit(channel_id, data);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment