Localhost disconnects automatically when a socket tries to connect
RupanRC opened this issue · 4 comments
XMLHttpRequest cannot load http://localhost/socket.io/?EIO=3&transport=polling&t=LLEzHd_. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 404.
This error pops out in the dev console.
If i change the port to 3000 Then in the console , a Get message is logged
HTTP server listening on port 3000
GET /favicon.ico 304 7.527 ms - -
Solved this by placing the socket.io.js locally in a socket.io folder
Now after everything The app closes while opening the web page localhost:3000
and the same thing happens in 8080 port too
"C:\Program Files (x86)\JetBrains11\WebStorm 2016.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" server.js
Camera started
HTTP server listening on port 3000
err
err
face Captured
image Captured // my console.logs
Process finished with exit code -1073740791 (0xC0000409)
It happens exactly when the page loads up
``
routes/socket.js:
var cv = require('../../lib/opencv');
// camera properties
var camWidth = 320;
var camHeight = 240;
var camFps = 10;
var camInterval = 1000 / camFps;
// face detection properties
var rectColor = [0, 255, 0];
var rectThickness = 2;
// initialize camera
var camera = new cv.VideoCapture(0);
camera.setWidth(camWidth);
camera.setHeight(camHeight);
console.log('Camera started');
module.exports = function (socket) {
setInterval(function() {
camera.read(function(err, im) {
if (err) throw err;
console.log('err');
im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) {
if (err) throw err;
for (var i = 0; i < faces.length; i++) {
face = faces[i];
im.rectangle([face.x, face.y], [face.width, face.height], rectColor, rectThickness);
}
console.log('face Captured');
im.save('/1.png');
console.log('Image saved to ./tmp/face-detection-rectangle.png');
socket.emit('frame', { buffer: im.toBuffer() });
});
});
}, camInterval);
};
`client/app.js:
// MODIFY THIS TO THE APPROPRIATE URL IF IT IS NOT BEING RUN LOCALLY
var socket = io.connect('http://localhost:port');
var canvas = document.getElementById('canvas-video');
var context = canvas.getContext('2d');
var img = new Image();
// show loading notice
context.fillStyle = '#333';
context.fillText('Loading...', canvas.width/2-30, canvas.height/3);
socket.on('frame', function (data) {
// Reference: http://stackoverflow.com/questions/24107378/socket-io-began-to-support-binary-stream-from-1-0-is-there-a-complete-example-e/24124966#24124966
var uint8Arr = new Uint8Array(data.buffer);
var str = String.fromCharCode.apply(null, uint8Arr);
var base64String = btoa(str);
img.onload = function () {
context.drawImage(this, 0, 0, canvas.width, canvas.height);
};
img.src = 'data:image/png;base64,' + base64String;
});
`
hi ~ i have the same problem with this issue.
have you solved the problem?
it was always automatically disconnect when i run browser "http://localhost:8080" or "http://localhost:3000" (change the port for client and server)
To anyone who may be having this issue, this is a possible fix, it worked for me. Note if you do this you will have to re-install the face detection package all over again. peterbraden/node-opencv#483
got this issue too.
is that mean if i use vs2017 and i can't use correspond opencv version? @FriendlyJOEY