1
+ 'use strict' ; /* jshint node:true */
1
2
var caronte = require ( './' ) ,
2
3
http = require ( 'http' ) ,
3
4
ws = require ( 'ws' ) ;
@@ -7,33 +8,38 @@ var proxyTo = new ws.Server({ port: 9090 });
7
8
proxyTo . on ( 'connection' , function ( ws ) {
8
9
console . log ( 'connection!' ) ;
9
10
ws . on ( 'message' , function ( msg ) {
10
- console . log ( 'received: ' + msg ) ;
11
+ ws . send ( 'ohai: ' + msg ) ;
12
+ setTimeout ( function ( ) {
13
+ ws . send ( 'HAHAHHA' ) ;
14
+ } , 10000 ) ;
11
15
} ) ;
12
16
ws . send ( 'derpity?' ) ;
13
17
} ) ;
14
18
15
- /* caronte.createProxyServer({
19
+ caronte . createProxyServer ( {
16
20
ws : true ,
17
21
target : 'http://127.0.0.1:9090'
18
- }).listen(8000);*/
22
+ } ) . listen ( 8000 ) ;
19
23
20
24
21
25
var client = new ws ( 'ws://127.0.0.1:8000' ) ;
22
26
client . on ( 'open' , function ( ) {
23
27
client . send ( 'baaaka' ) ;
24
28
console . log ( 'sent: baaaaka' ) ;
29
+ setTimeout ( function ( ) {
30
+ client . send ( 'cacca' ) ;
31
+ } , 5000 ) ;
32
+ client . on ( 'message' , function ( msg ) {
33
+ console . log ( 'server said: ' + msg ) ;
34
+ } ) ;
25
35
} ) ;
26
36
27
37
28
- var srv = http . createServer ( function ( req , res ) {
38
+ /* var srv = http.createServer(function(req, res) {
29
39
res.end('1');
30
40
}).listen(8000);
31
41
32
- srv . on ( 'connection' , function ( s ) {
33
- s . pipe ( process . stdout ) ;
34
- } ) ;
35
-
36
- srv . on ( 'upgrade' , function ( req , socket , head ) {
42
+ srv.on('upgrade', function(req, sock, head) {
37
43
38
44
var options = {
39
45
port: 9090,
@@ -42,21 +48,17 @@ srv.on('upgrade', function(req, socket, head) {
42
48
}
43
49
var r = http.request(options);
44
50
45
- r . on ( 'upgrade' , function ( res , sock , hd ) {
46
- if ( hd && hd . length ) sock . unshift ( hd ) ;
51
+ r.on('upgrade', function(res, proxySock , hd) {
52
+ if (hd && hd.length) proxySock .unshift(hd);
47
53
48
-
49
- socket . pipe ( sock ) . pipe ( socket ) ;
50
- //req.pipe(r).pipe(socket);
51
- /*console.log(hd.toString('utf-8'));
52
- var str = Object.keys(res.headers).map(function(i) {
54
+ sock.write('HTTP/1.1 101 Switching Protocols\r\n');
55
+ sock.write(Object.keys(res.headers).map(function(i) {
53
56
return i + ": " + res.headers[i];
54
- }).join('\r\n');
55
- socket.write("HTTP/1.1 101 Switching Protocols\r\n" + str);
56
-
57
- socket.write(hd);
58
- socket.pipe(sock).pipe(socket);*/
57
+ }).join('\r\n') + '\r\n\r\n');
58
+ proxySock.pipe(sock).pipe(proxySock);
59
59
});
60
60
61
61
r.end();
62
62
});
63
+
64
+ */
0 commit comments