I'm creating a chrome (packaged) app that would work with the irc protocol. So, here's what my listener looks like:
chrome.sockets.tcp.onReceive.addListener(function(info){
console.log("received data");
info.data = ab2str(info.data);
console.log(info);
})
ab2str
should convert ArrayBuffers to strings. I've copied it from html5rocks:
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
But connect to irc.freenode.net
on port 8002, I get weird strings of characters in the log, and on some occasions, I get the following error:
Error in event handler for sockets.tcp.onReceive: RangeError: bofyshould be a multiple ofe
at new Uint16Array (native)
at ab2str (chrome-extension://jkmacdefiplofpdmgimbaobepnigcchm/js/protocol.js:80:41)
at chrome-extension://jkmacdefiplofpdmgimbaobepnigcchm/js/protocol.js:73:15
...
Most of the stack trace looks like internal chrome code, so I left it out.
What am I doing wrong? Should I not listen for any data before sending the USER and NICK messages?
I've made sure that I'm connecting to freenode on a port that doesn't require ssl, as described in this page about freenode's servers
IRC is an ascii protocol, so I should interpret single bytes as characters, which I can do by replacing Uint16Array
with Uint8Array