Skip to content

Commit 3367082

Browse files
committedAug 18, 2014
Fixes to pass the whole URL to the connection so as to get host and path. Also fixed the handshaking code since it never seemed to hit.
1 parent 590c95a commit 3367082

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed
 

‎Connection.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
// parent can be a Server or, in case of client connection, a string with the path to connect to
77
// callback will be added as a listener to "connect"
88
// Events: close(code, reason), error(err), text(str), binary(inStream), connect()
9-
function Connection(socket, parent, callback) {
9+
function Connection(socket, url, callback) {
1010
var that = this
1111

12+
this.host = url.host
1213
this.socket = socket
13-
this.server = typeof parent == "string" ? null : parent
14+
this.server = typeof url.path == "string" ? null : url.path
1415
this.readyState = this.CONNECTING
1516
this.buffer = "" // string before handshake, Buffer after that
1617
this.frameBuffer = null // string for text frames and InStream for binary frames
1718
this.outStream = null // current allocated OutStream object for sending binary frames
18-
this.path = typeof parent == "string" ? parent : null
19+
this.path = typeof url.path == "string" ? url.path : null
1920
this.key = null // the Sec-WebSocket-Key header
2021

2122
// Set listeners
@@ -26,9 +27,7 @@ function Connection(socket, parent, callback) {
2627
that.emit("error", err)
2728
})
2829
if (!this.server)
29-
socket.on("connect", function () {
30-
that.startHandshake()
31-
})
30+
that.startHandshake()
3231

3332
// Close listeners
3433
var onclose = function () {
@@ -169,13 +168,13 @@ Connection.prototype.startHandshake = function () {
169168
key = new Buffer(16)
170169
for (i=0; i<16; i++)
171170
key[i] = Math.floor(Math.random()*256)
172-
this.key = key.toString("base64")
171+
this.key = key.toString("base64");
173172
str = "GET "+this.path+" HTTP/1.1\r\n"+
174-
"Host: "+this.parent+"\r\n"+
173+
"Host: "+this.host+"\r\n"+
175174
"Upgrade: websocket\r\n"+
176-
"Connection: Upgrade\r\n"+
175+
"Connection: UCpgrade\r\n"+
177176
"Sec-WebSocket-Key: "+this.key+"\r\n"+
178-
"Sec-WebSocket-Version: 13\r\n\r\n"
177+
"Sec-WebSocket-Version: 13\r\n\r\n";
179178
this.socket.write(str)
180179
}
181180

‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ exports.connect = function (URL, options, callback) {
3636
else
3737
socket = net.connect(options)
3838

39-
return new Connection(socket, URL.path, callback)
39+
return new Connection(socket, URL, callback)
4040
}
4141

4242
// Set the minimum size of a pack of binary data to send in a single frame

0 commit comments

Comments
 (0)