Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the variable req? #85

Open
marchah opened this issue Jul 18, 2014 · 0 comments
Open

How to get the variable req? #85

marchah opened this issue Jul 18, 2014 · 0 comments

Comments

@marchah
Copy link

marchah commented Jul 18, 2014

Hello,

I need to get the variable 'req' with a requete to the BinaryJS server.

First I split 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 with my implementation:

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://5.135.163.236:8080/binary-endpoint [168ms]
Firefox can't establish a connection to the server at ws://5.135.163.236: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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant