I have the following code
var https = require("https");
var fs = require("fs");
var server = https.createServer({
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
}, function (req, res) {
res.writeHead(200);
res.end(req.url);
});
server.listen(8765, "0.0.0.0", function() {
console.log("listening");
});
server.on("connection", function(socket) {
console.log("socket connected", socket.remotePort);
socket.on("data", function() {
console.log("socket data");
});
socket.on("close", function() {
console.log("socket closed");
});
socket.setTimeout(3000);
setInterval(function() {
console.log("socket destroyed: ", socket.destroyed);
}, 500);
});
When I make a request to the app, the "socket connected PORT" logs to console, but neither "socket data" nor "socket closed" ever logs. To make sure my event listeners aren't ignored, I set up a polling on socket.destroyed
but it always logs false
, no matter how much I wait.
I've made the request using chrome, safari, firefox, curl, but every one of my request gives the same result. I also did a Wireshark analysis and the client does indeed send a FIN packet and the server responds to it with its own FIN packet.
My question is, how can I get notified when the https socket closes? Is this socket some kind of proxy object made by the cypher to create clear text stream?
Update
The socket that NodeJS uses does close, as the server.connections
changes as expected and if I call server.close(callback)
, the callback does get executed and the process terminates gracefully.
From node documentation
"When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed. The user must manually end() or destroy() the socket."
This probably means that it will not dispatch close
event because the connection is not really terminated for it.
Try also to listen to end
event instead of close
.
server.on("connection", function(socket) {
socket.setEncoding('UTF-8');
console.log("socket connected", socket.remotePort);
socket.on("data", function() {
console.log("socket data");
});
socket.on("end", function() {
console.log("socket closed");
});
socket.setTimeout(3000, function(){
socket.end();
});
setInterval(function() {
console.log("socket destroyed: ", socket.destroyed);
}, 500);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.