I'm doing an upload/streaming server with nodeJS, ExpressJS and BinaryJS for the websocket. Separately, everything works good but the user has to be authentified to upload a video and that's my problem so I need the variable 'req'.
When I use BinaryJS on my express server, I don't have access to the variable 'req' at it is specified in the doc (https://github.com/binaryjs/binaryjs/blob/master/doc/api.md, https://github.com/binaryjs/binaryjs/blob/master/doc/start.md) I use an endpoint but that doesn't work.
When I split the server the ExpressJS server and the BinaryJS server: Server:
var express = require('express');
var app = express();
var BinaryServer = require('binaryjs').BinaryServer;
app.listen(8080);
var bs = new BinaryServer({port: 9000});
function request(client, meta) {
console.log("request");
}
function upload(stream, meta) {
console.log("upload");
stream.write({ end: true });
}
bs.on('connection', function (client) {
client.on('stream', function (stream, meta) {
switch(meta.event) {
case 'stream':
request(client, meta);
break;
case 'upload':
upload(stream, meta);
}
});
});
console.log('The magic happens on port ' + port);
Client:
<html>
<body>
<div>
<div>
<input type="file" name="video" id="video" />
<p id="progress"></p>
<button type="submit" id="submit">Upload</button>
</div>
</div>
<script src="http://cdn.binaryjs.com/0/binary.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-migrate-1.2.1.min.js" />
<script>
var hostname = window.location.hostname;
var client = new BinaryClient('ws://' + hostname + ':9000');
$('#submit').click(function(){
var file, tx;
file = $('#video')[0].files[0];
tx = 0;
upload(file, function (err, data) {
if (data.end) {
console.log("Upload complete: " + file.name);
} else if (data.rx) {
console.log(Math.round(tx += data.rx * 100) + '% complete');
} else {
console.log(data.err);
}
});
});
function upload(file, cb) {
var stream = client.send(file, {name : file.name, size : file.size, type : file.type, event : 'upload'});
stream.on('data', function (data) {
cb(null, data);
});
stream.on('error', cb);
}
</script>
</body>
</html>
This code works perfectly but i don't have access to the variable 'req', so I did like it is specified in the documentation but I am probably wrong:
Server:
var express = require('express');
var app = express();
var BinaryServer = require('binaryjs').BinaryServer;
app.listen(8080);
var bs = new BinaryServer({port: 9000});
function request(client, meta) {
console.log("request");
}
function upload(stream, meta) {
console.log("upload");
stream.write({ end: true });
}
app.get('/binary-endpoint', function(req, res) {
console.log("get");
});
app.post('/binary-endpoint', function(req, res) {
console.log("post");
});
bs.on('connection', function (client) {
client.on('stream', function (stream, meta) {
switch(meta.event) {
case 'stream':
request(client, meta);
break;
case 'upload':
upload(stream, meta);
}
});
});
console.log('The magic happens on port ' + port);
Just this line changes in the client:
var client = new BinaryClient('ws://' + hostname + ':8080/binary-endpoint');
But nothing happen in the server, no log is diplayed and I get this message in the client console:
GET http://127.0.0.1/upload.html [HTTP/1.1 304 Not Modified 1ms]
GET http://cdn.binaryjs.com/0/binary.js [HTTP/1.1 304 Not Modified 102ms]
GET http://code.jquery.com/jquery-1.11.0.min.js [HTTP/1.1 304 Not Modified 26ms]
GET http://code.jquery.com/jquery-migrate-1.2.1.min.js [HTTP/1.1 304 Not Modified 48ms]
GET http://localhost:8080/binary-endpoint [168ms]
Firefox can't establish a connection to the server at ws://localhost:8080/binary-endpoint. binary.js:1341
GET http://null/ [2252ms]
Error: Client is not yet connected or has closed binary.js:1539
Firefox can't establish a connection to the server at ws://null/.
Thanks for your help