Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Individual bullet connections in the example can be enabled/disabled.

This illustrates how to close bullet connections.
  • Loading branch information...
commit e8535ec4b8316effd3da3ce9622ae931e251531a 1 parent d4d541a
@jdavisp3 jdavisp3 authored
Showing with 35 additions and 19 deletions.
  1. +35 −19 examples/clock/src/toppage_handler.erl
View
54 examples/clock/src/toppage_handler.erl
@@ -20,13 +20,17 @@ handle(Req, State) ->
</head>
<body>
- <p>Current time (best source): <span id=\"time_best\">unknown</span>
+ <p><input type=\"checkbox\" checked=\"yes\" id=\"enable_best\"></input>
+ Current time (best source): <span id=\"time_best\">unknown</span>
<span> </span><span id=\"status_best\">unknown</span></p>
- <p>Current time (websocket only): <span id=\"time_websocket\">unknown</span>
+ <p><input type=\"checkbox\" checked=\"yes\" id=\"enable_websocket\"></input>
+ Current time (websocket only): <span id=\"time_websocket\">unknown</span>
<span> </span><span id=\"status_websocket\">unknown</span></p>
- <p>Current time (eventsource only): <span id=\"time_eventsource\">unknown</span>
+ <p><input type=\"checkbox\" checked=\"yes\" id=\"enable_eventsource\"></input>
+ Current time (eventsource only): <span id=\"time_eventsource\">unknown</span>
<span> </span><span id=\"status_eventsource\">unknown</span></p>
- <p>Current time (polling only): <span id=\"time_polling\">unknown</span>
+ <p><input type=\"checkbox\" checked=\"yes\" id=\"enable_polling\"></input>
+ Current time (polling only): <span id=\"time_polling\">unknown</span>
<span> </span><span id=\"status_polling\">unknown</span></p>
<script
@@ -37,22 +41,34 @@ handle(Req, State) ->
// <![CDATA[
$(document).ready(function(){
var start = function(name, options) {
- var bullet = $.bullet('ws://localhost:8080/bullet', options);
- bullet.onopen = function(){
- $('#status_' + name).text('online');
- };
- bullet.ondisconnect = function(){
- $('#status_' + name).text('offline');
- };
- bullet.onmessage = function(e){
- if (e.data != 'pong'){
- $('#time_' + name).text(e.data);
- }
- };
- bullet.onheartbeat = function(){
- console.log('ping: ' + name);
- bullet.send('ping: ' + name);
+ var bullet;
+ var open = function(){
+ bullet = $.bullet('ws://localhost:8080/bullet', options);
+ bullet.onopen = function(){
+ $('#status_' + name).text('online');
+ };
+ bullet.onclose = bullet.ondisconnect = function(){
+ $('#status_' + name).text('offline');
+ };
+ bullet.onmessage = function(e){
+ if (e.data != 'pong'){
+ $('#time_' + name).text(e.data);
+ }
+ };
+ bullet.onheartbeat = function(){
+ console.log('ping: ' + name);
+ bullet.send('ping: ' + name);
+ };
}
+ open();
+ $('#enable_' + name).on('change', function(){
+ if (this.checked){
+ open();
+ } else{
+ bullet.close();
+ bullet = null;
+ }
+ });
};
start('best', {});

3 comments on commit e8535ec

@cauburtin

nice work, just tested, only little bug: poll never stops, seems bullet.close() doesn't work well for it
edit: doesn't happen with a poll timeout of 5000, but does with 100

@essen
Owner

Please open a ticket.

@jdavisp3

Try out #35.

Please sign in to comment.
Something went wrong with that request. Please try again.