WS on sencha connect #3

Closed
opened 2014-03-06 18:19:09 +00:00 by rumkin · 3 comments
rumkin commented 2014-03-06 18:19:09 +00:00 (Migrated from github.com)

Is it possible to integrate nodejs-websocket with connect (or express) https server to run on the same port?

Is it possible to integrate nodejs-websocket with connect (or express) https server to run on the same port?
scottstensland commented 2014-08-08 21:21:20 +00:00 (Migrated from github.com)

@rumkin this is a very valid question - I too am interested in hearing the answer and seeing some example usage if possible this this lib - the existing chat sample code uses two ports - evidently one port is possible if only one server uses it - which then forwards the traffic based on whether its a web socket or http

@rumkin this is a very valid question - I too am interested in hearing the answer and seeing some example usage if possible this this lib - the existing chat sample code uses two ports - evidently one port is possible if only one server uses it - which then forwards the traffic based on whether its a web socket or http
sitegui commented 2014-08-08 22:28:32 +00:00 (Migrated from github.com)

@rumkin nice question! The nodejs-websocket was built on top of TCP, not HTTP (express).

This means this module expects to read the handshake headers on bare TCP and this would conflict a lot with connect/express.

I wonder why this would be needed, I believe it's better to keep them separated.

In any case, a solution that I can think of is proxing from express to another port. Something like:

// app is an express instance
// this will proxy all data from /ws to your local ws server
app.use('/ws', function (req, res) {
    net.connect(WS_PORT, 'localhost', function (conn) {
        // Send the handshake again (see bellow)
        conn.write(recreateWSHandshake(req))
        // Proxy
        conn.pipe(req.socket)
        req.socket.pipe(conn)
    })
    // TODO: handle errors
})

(I have not tested this code!)

The idea is to create the WS on a diferent port (it can be a virtual port in this case) and proxy the underlying TCP captured from express to a new bare connection to WS server.

The recreateWSHandshake would be something like:

function recreateWSHandshake(req) {
    return "GET "+req.url+" HTTP/1.1\r\n"+
        "Host: "+req.headers.host+"\r\n"+
        "Upgrade: websocket\r\n"+
        "Connection: Upgrade\r\n"+
        "Sec-WebSocket-Key: "+req.headers["sec-websocket-accept"]+"\r\n"+
        "Sec-WebSocket-Version: 13\r\n\r\n"
}

Tell me if it works :)

@rumkin nice question! The nodejs-websocket was built [on top of TCP](https://github.com/sitegui/nodejs-websocket/blob/master/Server.js#L39), not HTTP (express). This means this module expects to read the handshake headers [on bare TCP](https://github.com/sitegui/nodejs-websocket/blob/master/Connection.js#L225) and this would conflict a lot with connect/express. I wonder why this would be needed, I believe it's better to keep them separated. In any case, a solution that I can think of is proxing from express to another port. Something like: ``` javascript // app is an express instance // this will proxy all data from /ws to your local ws server app.use('/ws', function (req, res) { net.connect(WS_PORT, 'localhost', function (conn) { // Send the handshake again (see bellow) conn.write(recreateWSHandshake(req)) // Proxy conn.pipe(req.socket) req.socket.pipe(conn) }) // TODO: handle errors }) ``` (I have not tested this code!) The idea is to create the WS on a diferent port (it can be a virtual port in this case) and proxy the underlying TCP captured from express to a new bare connection to WS server. The recreateWSHandshake would be something like: ``` javascript function recreateWSHandshake(req) { return "GET "+req.url+" HTTP/1.1\r\n"+ "Host: "+req.headers.host+"\r\n"+ "Upgrade: websocket\r\n"+ "Connection: Upgrade\r\n"+ "Sec-WebSocket-Key: "+req.headers["sec-websocket-accept"]+"\r\n"+ "Sec-WebSocket-Version: 13\r\n\r\n" } ``` Tell me if it works :)
rndmania commented 2015-12-24 03:40:29 +00:00 (Migrated from github.com)

i have changed some part of your example.

// "sec-websocket-accept" -> "sec-websocket-key"
function recreateWSHandshake(req) {
return "GET "+req.url+" HTTP/1.1\r\n"+
"Host: "+req.headers.host+"\r\n"+
"Upgrade: websocket\r\n"+
"Connection: Upgrade\r\n"+
"Sec-WebSocket-Key: "+req.headers["sec-websocket-key"]+"\r\n"+
"Sec-WebSocket-Version: 13\r\n\r\n"
}

// 'server' is 'http' module`s instance can used with express
server.on('upgrade', function(req, socket, head) {
var client = net.connect({port: 4000}, function () {
// Send the handshake again (see bellow)
client.write(recreateWSHandshake(req))
// Proxy
client.pipe(req.socket)
req.socket.pipe(client)
});
socket.once('close', function(e) {
client.destroy();
});
});

It works well. but this approach needs so many ports

i have changed some part of your example. // "sec-websocket-accept" -> "sec-websocket-key" function recreateWSHandshake(req) { return "GET "+req.url+" HTTP/1.1\r\n"+ "Host: "+req.headers.host+"\r\n"+ "Upgrade: websocket\r\n"+ "Connection: Upgrade\r\n"+ "Sec-WebSocket-Key: "+req.headers["sec-websocket-key"]+"\r\n"+ "Sec-WebSocket-Version: 13\r\n\r\n" } // 'server' is 'http' module`s instance can used with express server.on('upgrade', function(req, socket, head) { var client = net.connect({port: 4000}, function () { // Send the handshake again (see bellow) client.write(recreateWSHandshake(req)) // Proxy client.pipe(req.socket) req.socket.pipe(client) }); socket.once('close', function(e) { client.destroy(); }); }); It works well. but this approach needs so many ports
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: sitegui/nodejs-websocket#3
No description provided.