我正在为websocket做一个带nodeJS,ExpressJS和BinaryJS的上传/流媒体服务器。另外,一切都运行良好,但用户必须进行身份验证才能上传视频,这就是我的问题所以我需要变量'req'。
当我在我的快速服务器上使用BinaryJS时,我无法访问变量'req',它在文档中指定(https://github.com/binaryjs/binaryjs/blob/master/doc/api.md,https://github.com/binaryjs/binaryjs/blob/master/doc/start.md)我使用端点但是不起作用。
当我将服务器拆分为ExpressJS服务器和BinaryJS服务器时: 服务器:
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);
客户端:
<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>
这段代码完美无缺,但是我无法访问变量'req',所以我确实喜欢它在文档中指定,但我可能错了:
服务器:
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);
这条线路在客户端中发生了变化:
var client = new BinaryClient('ws://' + hostname + ':8080/binary-endpoint');
但是没有任何事情发生在服务器上,没有显示日志,我在客户端控制台中收到此消息:
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/.
感谢您的帮助