Permalink
Browse files

Merge branch 'eventsource-xhr-send' of git://github.com/jdavisp3/bullet

  • Loading branch information...
essen committed Jun 29, 2013
2 parents 22c1d0a + 9536627 commit 458884c543a99f7c09438fd5989bec0802b42976
Showing with 57 additions and 38 deletions.
  1. +14 −4 examples/clock/src/toppage_handler.erl
  2. +43 −34 priv/bullet.js
@@ -22,16 +22,20 @@ handle(Req, State) ->
<body>
<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>
+ <span></span><span id=\"status_best\">unknown</span>
+ <button id=\"send_best\">Send Time</button></p>
<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>
+ <span></span><span id=\"status_websocket\">unknown</span>
+ <button id=\"send_websocket\">Send Time</button></p>
<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>
+ <span></span><span id=\"status_eventsource\">unknown</span>
+ <button id=\"send_eventsource\">Send Time</button></p>
<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>
+ <span></span><span id=\"status_polling\">unknown</span>
+ <button id=\"send_polling\">Send Time</button></p>
<script
src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\">
@@ -69,6 +73,12 @@ $(document).ready(function(){
bullet = null;
}
});
+ $('#send_' + name).on('click', function(){
+ if (bullet) {
+ bullet.send('time: ' + name + ' '
+ + $('#time_' + name).text());
+ }
+ });
};
start('best', {});
View
@@ -38,6 +38,36 @@
var CLOSING = 2;
var CLOSED = 3;
+ var xhrSend = function(data){
+ /**
+ Send a message using ajax. Used for both the
+ eventsource and xhrPolling transports.
+ */
+ if (this.readyState != CONNECTING && this.readyState != OPEN){
+ return false;
+ }
+
+ var sendUrl = url.replace('ws:', 'http:').replace('wss:', 'https:');
+
+ $.ajax({
+ async: false,
+ cache: false,
+ type: 'POST',
+ url: sendUrl,
+ data: data,
+ dataType: 'text',
+ contentType: 'application/x-www-form-urlencoded; charset=utf-8',
+ headers: {'X-Socket-Transport': 'xhrPolling'},
+ success: function(data){
+ if (data.length !== 0){
+ fake.onmessage({'data': data});
+ }
+ }
+ });
+
+ return true;
+ };
+
var transports = {
/**
The websocket transport is disabled for Firefox 6.0 because it
@@ -96,9 +126,7 @@
var fake = {
readyState: CONNECTING,
- send: function(data){
- return false; // fallback to another method instead?
- },
+ send: xhrSend,
close: function(){
fake.readyState = CLOSED;
source.close();
@@ -116,39 +144,17 @@
}
var timeout;
- var xhr;
+ var xhr = null;
var fake = {
readyState: CONNECTING,
- send: function(data){
- if (this.readyState != CONNECTING && this.readyState != OPEN){
- return false;
- }
-
- var fakeurl = url.replace('ws:', 'http:').replace('wss:', 'https:');
-
- $.ajax({
- async: false,
- cache: false,
- type: 'POST',
- url: fakeurl,
- data: data,
- dataType: 'text',
- contentType:
- 'application/x-www-form-urlencoded; charset=utf-8',
- headers: {'X-Socket-Transport': 'xhrPolling'},
- success: function(data){
- if (data.length != 0){
- fake.onmessage({'data': data});
- }
- }
- });
-
- return true;
- },
+ send: xhrSend,
close: function(){
this.readyState = CLOSED;
- xhr.abort();
+ if (xhr){
+ xhr.abort();
+ xhr = null;
+ }
clearTimeout(timeout);
fake.onclose();
},
@@ -169,19 +175,21 @@
data: {},
headers: {'X-Socket-Transport': 'xhrPolling'},
success: function(data){
+ xhr = null;
if (fake.readyState == CONNECTING){
fake.readyState = OPEN;
fake.onopen(fake);
}
// Connection might have closed without a response body
- if (data.length != 0){
+ if (data.length !== 0){
fake.onmessage({'data': data});
}
if (fake.readyState == OPEN){
nextPoll();
}
},
error: function(xhr){
+ xhr = null;
fake.onerror();
}
});
@@ -257,8 +265,9 @@
};
transport.onclose = function(){
// Firefox 13.0.1 sends 2 close events.
- // Return directly if we already handled it.
- if (isClosed){
+ // Return directly if we already handled it
+ // or we are closed
+ if (isClosed || readyState == CLOSED){
return;
}

0 comments on commit 458884c

Please sign in to comment.