File tree Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change 28
28
< body >
29
29
< script type ="text/javascript " src ="/js/libs/jquery-2.1.3.js "> </ script >
30
30
< script type ="text/javascript ">
31
- var $body = $ ( 'body' ) ;
31
+ var $body = $ ( 'body' ) ,
32
+ connected = false ,
33
+ domain ,
34
+ user ;
32
35
33
36
$ . getJSON ( 'http://localhost:7000/config' , function ( resp ) {
34
37
// resp should contain the username and root_domain
35
- connect_ws ( resp . root_domain , resp . username ) ;
38
+ domain = resp . root_domain ;
39
+ user = resp . username ;
40
+ setInterval ( check_connection , 5000 ) ;
41
+ check_connection ( ) ;
36
42
} ) ;
37
43
44
+ function check_connection ( ) {
45
+ if ( ! connected ) {
46
+ connect_ws ( domain , user ) ;
47
+ }
48
+ }
49
+
38
50
// initializes WebSocket connection
39
51
function connect_ws ( root_domain , username ) {
40
52
console . log ( 'init WebSocket connection as ' + username ) ;
41
53
var ws = new WebSocket ( "ws://" + root_domain + "/ws/" + username ) ;
42
54
ws . onopen = function ( ) {
55
+ console . log ( 'connection open' ) ;
43
56
ws . send ( "connecting as " + username ) ;
57
+ connected = true ;
44
58
} ;
45
59
ws . onmessage = function ( evt ) {
46
60
var data = JSON . parse ( evt . data ) ;
57
71
} , 1000 ) ;
58
72
} ) ;
59
73
}
60
-
74
+ } ;
75
+ ws . onclose = function ( evt ) {
76
+ console . log ( 'connection closed' ) ;
77
+ connected = false ;
61
78
} ;
62
79
}
63
80
</ script >
You can’t perform that action at this time.
0 commit comments