I'm trying to create a socket between my web page and a nodejs server using the method CONNECTION. I have already succeeded in creating the server and client with nodejs (I attach the code below)
The problem is that I do not know how to do a CONNECT call, I have tried with XMLHttpRequest but a security exception (unsupported operation) is thrown when I make the request.
How can I do it?
Thanks in advance
Web client:
var request = new XMLHttpRequest();
request.open('CONNECT',"localhost:1337",true)
exception thrown:
Uncaught SecurityError: Failed to execute 'open' on 'XMLHttpRequest': 'CONNECT' HTTP method is unsupported.
SimpleCliente.js:
var http = require('http');
var options = {
port: 1337,
hostname: '127.0.0.1',
method: 'CONNECT'
};
var req = http.request(options);
req.end();
req.on('connect', function(res, socket, head) {
console.log('got connected!');
socket.on('data', function(chunk) {
console.log(chunk.toString());
});
socket.on('end', function() {
console.log("Finished")
});
});
SimpleServer.js
var http = require('http');
var fs = require ('fs')
// Create an HTTP tunneling proxy
var proxy = http.createServer(function (req, res) {
console.log("creating server function")
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
});
proxy.on('connect', function(req, cltSocket, head) {
console.log("connect function")
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node-Proxy\r\n' +
'\r\n');
var rdStream= fs.createReadStream("../dashboard/data/random-data/100/csv0.csv");
rdStream.pipe(cltSocket);
});
proxy.listen(1337)