-
Notifications
You must be signed in to change notification settings - Fork 155
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
WS on sencha connect #3
Comments
@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 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 :) |
i have changed some part of your example. // "sec-websocket-accept" -> "sec-websocket-key" // 'server' is 'http' module`s instance can used with express It works well. but this approach needs so many ports |
Is it possible to integrate nodejs-websocket with connect (or express) https server to run on the same port?
The text was updated successfully, but these errors were encountered: