6
6
// parent can be a Server or, in case of client connection, a string with the path to connect to
7
7
// callback will be added as a listener to "connect"
8
8
// Events: close(code, reason), error(err), text(str), binary(inStream), connect()
9
- function Connection ( socket , parent , callback ) {
9
+ function Connection ( socket , url , callback ) {
10
10
var that = this
11
11
12
+ this . host = url . host
12
13
this . socket = socket
13
- this . server = typeof parent == "string" ? null : parent
14
+ this . server = typeof url . path == "string" ? null : url . path
14
15
this . readyState = this . CONNECTING
15
16
this . buffer = "" // string before handshake, Buffer after that
16
17
this . frameBuffer = null // string for text frames and InStream for binary frames
17
18
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
19
20
this . key = null // the Sec-WebSocket-Key header
20
21
21
22
// Set listeners
@@ -26,9 +27,7 @@ function Connection(socket, parent, callback) {
26
27
that . emit ( "error" , err )
27
28
} )
28
29
if ( ! this . server )
29
- socket . on ( "connect" , function ( ) {
30
- that . startHandshake ( )
31
- } )
30
+ that . startHandshake ( )
32
31
33
32
// Close listeners
34
33
var onclose = function ( ) {
@@ -169,13 +168,13 @@ Connection.prototype.startHandshake = function () {
169
168
key = new Buffer ( 16 )
170
169
for ( i = 0 ; i < 16 ; i ++ )
171
170
key [ i ] = Math . floor ( Math . random ( ) * 256 )
172
- this . key = key . toString ( "base64" )
171
+ this . key = key . toString ( "base64" ) ;
173
172
str = "GET " + this . path + " HTTP/1.1\r\n" +
174
- "Host: " + this . parent + "\r\n" +
173
+ "Host: " + this . host + "\r\n" +
175
174
"Upgrade: websocket\r\n" +
176
- "Connection: Upgrade \r\n" +
175
+ "Connection: UCpgrade \r\n" +
177
176
"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" ;
179
178
this . socket . write ( str )
180
179
}
181
180
0 commit comments