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

upgrade event does not fired #2556

Closed
StreetStrider opened this issue Feb 20, 2015 · 3 comments
Closed

upgrade event does not fired #2556

StreetStrider opened this issue Feb 20, 2015 · 3 comments
Assignees
Labels

Comments

@StreetStrider
Copy link

It seemes that express@4 and express@3 does not fire upgrade event when client WebSocket requests it. Here's the code. Let's first try to use std http module:

var
    http = require('http');

var server = http
.createServer(function (rq, rs)
{
    rs.end('<title>test page</title>');
})
.listen(8080, console.log.bind(console, 'server started'));

server.on('upgrade', function (_, socket)
{
    console.log('upgrade event fired', socket);
});

On client:

new WebSocket('ws://localhost:8080');

This works. Server logs upgrade event, client does not throw any errors. Now, let's replace http with express. Here's the analogous code with required adaptations:

var
    server = require('express')();

server.get('/', function (rq, rs)
{
    rs.end('<title>test page</title>')
})
.listen(8080, console.log.bind(console, 'server started'));

server.on('upgrade', function (_, socket)
{
    console.log('upgrade event fired', socket);
});

The client is the same. This code does not work: server does not fire event, and client throws handshake error.

I'm trying to integrate express with ws module. It can use express instance and relies on upgrade event. I've logged through its code, and looks like error somewhere on express side. Or maybe I don't understand express API properly. Anyway, I'm looking for help.

@dougwilson
Copy link
Contributor

Express is an app, not an http server. Your code should be as follows:

var
    http = require('http');
var
    app = require('express')();

app.get('/', function (rq, rs)
{
    rs.end('<title>test page</title>')
})

var server = http
.createServer(app)
.listen(8080, console.log.bind(console, 'server started'));

server.on('upgrade', function (_, socket)
{
    console.log('upgrade event fired', socket);
});

@dougwilson dougwilson self-assigned this Feb 20, 2015
@StreetStrider
Copy link
Author

@dougwilson, this helps, thanks for clarification.

@emanavas
Copy link

For me work if I do a connection to port 8080 on client side.

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

No branches or pull requests

3 participants