如何制作节点。js TCP服务器写入数据库

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (100)

我有通过TCP发送实时数据的软件客户端。而nodejs是TCP服务器监听它并写入mysql DB。当尝试实现代码我得到运行时错误需要帮助。

==============================代码=================== ================

net.createServer(function (sock){ // We have a connection - a socket object is assigned to the connection automatically 
    console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort); // Identify this client 
    sock.name = sock.remoteAddress + ":" + sock.remotePort ; 
    var chunk = ""; 
    sock.on('data', function (data) { 
     console.log('DATA ' + sock.remoteAddress + ': ' + data); // Write the data back to the socket, the client will receive it as data from the server 
     chunk += data.toString(); // Add string on the end of the variable 'chunk' // save log to database 
     pool.getConnection(function (err, conn) {
        if (err) throw err; conn.query('INSERT INTO DB (Table) VALUES(?)', [chunk] , 
        function (err, result) { 
               if (err) throw err; 
               console.log(error.code); 
               console.log("Inserted"); 
          }); 
    });

===========运行时错误==============

/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
Error: ER_DATA_TOO_LONG: Data too long for column 'Table' at row 1
at Query.Sequence._packetToError (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Protocol.js:38:16)
at Socket. (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/Connection.js:91:28)
at Socket. (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/Connection.js:525:10)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
--------------------
at Protocol._enqueue (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Protocol.js:144:48)
at PoolConnection.query (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/Connection.js:201:25)
at /home/jothi/WorkSpaces/javaScript/Node/TCP/server/tcpmysql.js:36:11
at Handshake.onConnect (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/Pool.js:64:7)
at Handshake. (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/Connection.js:525:10)
at Handshake._callback (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/Connection.js:491:16)
at Handshake.Sequence.end (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/sequences/Sequence.js:83:24)
at Handshake.Sequence.OkPacket (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/sequences/Sequence.js:92:8)
at Protocol._parsePacket (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/jothi/WorkSpaces/javaScript/Node/TCP/server/mysql/lib/protocol/Parser.js:433:10)

请帮我

提问于
用户回答回答于
chunk += data.toString();

块包含来自您收到的每个数据包的数据,因为它从未被重置。考虑使用=而不是+=

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励