using node"s net module
, the format of transmission is expected to be json, but net.write only supports buffer and strings
, so I use json.stringify to format object data
when running a chat room, the first user logs in and prints normally, while the second user reports an error because of the data method of monitoring, and the data sent back is continuous
.as shown in the figure: abbreviated writing
server
var json = JSON.stringify
socket.write(json ("1"))
socket.write(json ("2"))
socket.write(json ("3"))
client
socket.on("data", function (data) {
data = JSON.parse(data);
console.log(data )
});
where message and userList call socket.write, successively, such as:
socket.write ({type: "message",.})
socket.write ({type:" userList",.})
how to implement a write, client to print once, instead of accepting it all and then triggering the function to print.
and why it can be printed successfully the first time.