diff --git a/lib/websocket.js b/lib/websocket.js index aece46e..e2b1895 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -5,7 +5,11 @@ var events = require('events'); var http = require('http'); var net = require('net'); var urllib = require('url'); -var sys = require('sys'); +try{ + var sys = require('util'); +}catch(e){ // node v0.4.x + var sys = require('sys'); +} var FRAME_NO = 0; var FRAME_LO = 1; @@ -473,7 +477,17 @@ var WebSocket = function(url, proto, opts) { switch (getUrlScheme(url)) { case 'ws': var u = urllib.parse(url); - httpClient = http.createClient(u.port || 80, u.hostname); + var options = { + port: u.port || 80, + host: u.hostname, + path: u.path, + headers: httpHeaders + }; + try{ + httpClient = http.request(options); + }catch(e){ // node v0.4.x + httpClient = http.createClient(u.port || 80, u.hostname); + } httpPath = (u.pathname || '/') + (u.search || ''); httpHeaders.Host = u.hostname + (u.port ? (":" + u.port) : ""); break; @@ -581,7 +595,11 @@ var WebSocket = function(url, proto, opts) { errorListener(e); }); - var httpReq = httpClient.request(httpPath, httpHeaders); + try{ // node v0.4.x + var httpReq = httpClient.request(httpPath, httpHeaders); + }catch(e){ + var httpReq = httpClient; + } httpReq.write(challenge, 'binary'); httpReq.end();