http: event upgrade, how to I send a response?

343 views
Skip to first unread message

kyogron

unread,
Jan 5, 2013, 1:11:12 PM1/5/13
to nod...@googlegroups.com
Hi,

I want to implement a WebSocket server.

I am currently at the beginnings handling the HTTP Protocol upgrade handshake.

The handshake requires a response for security reasons unfortunately the "upgrade" event does not provide any response object or at least I cannot find one.

Is there an other way to response as a listener of the Event: 'upgrade' ?

Regards

Fedor Indutny

unread,
Jan 5, 2013, 1:15:38 PM1/5/13
to nod...@googlegroups.com
As you can see, 'upgrade' event listener receives `socket` object.

Try doing: socket.write('something').

Cheers,
Fedor.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

kyogron

unread,
Jan 5, 2013, 1:56:27 PM1/5/13
to nod...@googlegroups.com, fe...@indutny.com
Thanks for the quick response :)

var http = require('http');

var server = http.createServer();

server.on('request', function(req, res) {
    res.setHeader('Content-Type', 'text/plain');
    res.end('hello world');
});

server.on('upgrade', function(req, socket, head) {
    console.log('upgrading'); 
    socket.write('upgraded');
    socket.on('data', function() {
        console.log(arguments); 
    });
});

server.listen(3000);

in the chrome console:
var ws = new WebSocket('ws://localhost:3000);
ws.onopen = function() {
    ws.send('hello');
};


Except console.log('upgrading') there is no output at all.


The browser waits on a http response (it shows me pending and no response available).
The question is just how I could send one without a request obj supplied to the callback?

Fedor Indutny

unread,
Jan 5, 2013, 2:13:18 PM1/5/13
to kyogron, nod...@googlegroups.com
Well, I'd recommend you to learn how http upgrade works and to learn details of websocket protocol. Without knowing this it'd be very hard for you to continue.

Cheers,
Fedor.

mscdex

unread,
Jan 5, 2013, 2:25:51 PM1/5/13
to nodejs
kyogron, you might try using a node websocket module instead of doing
it yourself. Here's one: https://npmjs.org/package/ws

Bodo Kaiser

unread,
Jan 5, 2013, 2:33:16 PM1/5/13
to nod...@googlegroups.com
Now I know what to do:

You have to use socket.write("HTTP/1.1 101 Web Socket Protocol Handshake\r\n"...); actually it maybe also an idea to map the socket on a response object manually.

Am 05.01.2013 um 20:25 schrieb mscdex <msc...@gmail.com>:

kyogron, you might try using a node websocket module instead of doing
it yourself. Here's one: https://npmjs.org/package/ws

Reply all
Reply to author
Forward
0 new messages