Created
April 20, 2019 12:41
-
-
Save akinazuki/fba9bf80a9854c9a82a565c524e5b145 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tls = require('tls'); | |
// callback for when secure connection established | |
function connected(stream) { | |
if (stream) { | |
// socket connected | |
stream.write("GET / HTTP/1.0\n\rHost: encrypted.google.com:443\n\r\n\r"); | |
} else { | |
console.log("Connection failed"); | |
} | |
} | |
// needed to keep socket variable in scope | |
var dummy = this; | |
// try to connect to the server | |
dummy.socket = tls.connect(443, 'encrypted.google.com', function() { | |
// callback called only after successful socket connection | |
dummy.connected = true; | |
if (dummy.socket.authorized) { | |
// authorization successful | |
dummy.socket.setEncoding('utf-8'); | |
connected(dummy.socket); | |
} else { | |
// authorization failed | |
console.log(dummy.socket.authorizationError); | |
connected(null); | |
} | |
}); | |
dummy.socket.addListener('data', function(data) { | |
// received data | |
console.log(data); | |
}); | |
dummy.socket.addListener('error', function(error) { | |
if (!dummy.connected) { | |
// socket was not connected, notify callback | |
connected(null); | |
} | |
console.log("FAIL"); | |
console.log(error); | |
}); | |
dummy.socket.addListener('close', function() { | |
// do something | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment