我在node.js上编写了一个简单的TCP服务器,将一些数据发送到Chrome应用程序.在chrome应用程序中,当我获取数据时,我使用下面的函数将其转换为字符串,我得到一个异常" Uint16Array的字节长度应该是2的倍数 "
String.fromCharCode.apply(null, new Uint16Array(buffer))
Run Code Online (Sandbox Code Playgroud)
我找不到任何可能导致此问题以及如何解决此问题的信息.任何关于此的指示都非常感谢.
以下是node.js服务器中用于将数据发送到客户端的代码:
socket.on('data', function(data) {
console.log('DATA ' + socket.remoteAddress + ': ' + data);
// Write the data back to the socket,
// the client will receive it as data from the server
var r= socket.write('from server\r\n');
});
Run Code Online (Sandbox Code Playgroud)
以下是chrome app的代码:
chrome.sockets.tcp.onReceive.addListener(function (info) {
console.log('onListener registered');
if (info.socketId != socketid)
return;
else {
try {
data = ab2str(info.data);
console.log(data);
}
catch (e) {
console.log(e);
}
}
// info.data is an arrayBuffer.
});
function ab2str(buf) { …Run Code Online (Sandbox Code Playgroud)