我试用Chrome的
我使用
以下是代码片段:
sockets.tcpServer
API与本地应用程序进行通信。这是我的代码链接:http://pastebin.com/1mWXeZKr。我使用
nc 127.0.0.1 7000
测试服务器是否工作。然而,尽管我可以连接到服务器并从服务器接收消息,但服务器没有接收任何来自客户端(nc)发送的消息。我不确定是否正确使用API。有人能否注意到代码中的任何错误?以下是代码片段:
function onAccept(info) {
if (info.socketId != serverSocketId)
return;
console.log("Client connected.");
// Start receiving data.
_stringToArrayBuffer("connected.\n", function(buf) {
chrome.sockets.tcp.send(info.clientSocketId, buf, function(info) {
if (info.resultCode != 0)
console.log("Send failed.");
});
});
chrome.sockets.tcp.onReceive.addListener(onReceive);
}
function onReceive(info) {
console.log("Data received.");
_arrayBufferToString(info.data, function(str) {
console.log(str);
});
}
更新:
好的,我找到了解决方案。请见下面的答案。