Created
April 30, 2017 22:13
-
-
Save cantremember/955be8759e789d1e1565b69c438d925c to your computer and use it in GitHub Desktop.
Socket.io Client messaging using Binary Mode
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
/** | |
* 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