Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

Fix the lib for v0.5 and v0.6 of nodejs. It's also backward compatible with v0.4 #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ var events = require('events');
var http = require('http');
var net = require('net');
var urllib = require('url');
var sys = require('sys');

var reg = /^v0.([0-9]+)/i.exec(process.version);
var oldVersion = reg ? +reg[1] <= 4 : false;
var sys = oldVersion ? require('sys') : require('util');

var FRAME_NO = 0;
var FRAME_LO = 1;
Expand Down Expand Up @@ -473,9 +476,15 @@ var WebSocket = function(url, proto, opts) {
switch (getUrlScheme(url)) {
case 'ws':
var u = urllib.parse(url);
httpClient = http.createClient(u.port || 80, u.hostname);
httpPath = (u.pathname || '/') + (u.search || '');
httpHeaders.Host = u.hostname + (u.port ? (":" + u.port) : "");
var options = {
port: u.port || 80,
host: u.hostname,
path: httpPath,
headers: httpHeaders
};
httpClient = oldVersion ? http.createClient(u.port || 80, u.hostname) : http.request(options);
break;

case 'ws+unix':
Expand Down Expand Up @@ -581,7 +590,7 @@ var WebSocket = function(url, proto, opts) {
errorListener(e);
});

var httpReq = httpClient.request(httpPath, httpHeaders);
var httpReq = oldVersion ? httpClient.request(httpPath, httpHeaders) : httpClient;

httpReq.write(challenge, 'binary');
httpReq.end();
Expand Down