/*
jQuery Long Poll plugin 0.0.1 (02-05-2010)
Note: requires jQuery-JSONP: http://code.google.com/p/jquery-jsonp/
Copyright (c) 2010 JavaScript Guy
This script is licensed as free software under the terms of the
LGPL License: http://www.opensource.org/licenses/lgpl-license.php
*/
(function($){
$.longPoll = function( args ) {
args = args || {};
var self = $.extend( {
url: '', // URL To connect to
tsParameter: 'ts', // Parameter used for the timestamp
reconnect: true, // If we're automatically reconnecting
errorTimeout: 60, // Timeout for errors, eg: server timeout, execution time limit, etc.
errorTimeoutDelay: 5, // Seconds to delay, if the connection failed in an error
hasConnection: false, // If we have a connection
timestamp: 0,
error: false,
request: null,
reconnectFunc: function() {
if( self.reconnect ) {
self.disconnect();
// Reconnect - with longer error timeout delay (in case we have an intermittent connection)
if( self.error ) {
setTimeout( function() { self.connect(); }, self.errorTimeoutDelay * 1000 );
} else {
setTimeout( function() { self.connect(); }, 100 );
}
}
},
connect: function() {
self.reconnect = true;
// self.request = $.ajax( {
self.request = $.jsonp( {
url: self.url + '?' + self.tsParameter + '=' + self.timestamp + '&callback=?',
timeout: self.errorTimeout * 1000, // Timeout and reconnect
error: function(XHR, textStatus, errorThrown) {
self.reconnectFunc();
self.error = true;
},
dataType: "jsonp",
success: function( data ) {
self.timestamp = (data[self.tsParameter])? data[self.tsParameter]: self.timestamp;
self.success( data );
self.error = false;
},
complete: function( data ) {
self.reconnectFunc();
}
} );
self.hasConnection = true;
},
// Aborts request
disconnect: function() {
self.reconnect = false;
if( self.request )self.request.abort();
self.hasConnection = false;
},
// User defined
success: function( data ) {}
}, args );
return self;
};
})(jQuery);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JSONP Long Poll chat demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="content"></div>
<p>
<input type="button" name="connect" id="connect" value="Connect"/>
</p>
<p>
<input type="text" name="message" id="message" value="" />
<input type="button" name="sendMsg" id="sendMsg" value="Send"/>
</p>
<!--<script type="text/javascript" src="jquery-1.4.2.js"></script>-->
<!--<script type="text/javascript" src="jquery-jsonp-2.0.2.js"></script>-->
<script type="text/javascript" src="http://jquery-jsonp.googlecode.com/svn/trunk/core/jquery.jsonp.js"></script>
<!--<script type="text/javascript" src="jquery-longpoll-0.0.1.js"></script>-->
<script type="text/javascript">
// Chat app
var chatApp = function( url ) {
var self = {
connection: $.longPoll( {
url: url,
success: function( data ) {
// Ignore server timeouts, the script will handle it.
if( data['data'].indexOf( "Maximum execution time of" ) == -1 ) {
jQuery('#content').append( '<div>' + data['data'] + '</div>' );
}
}
} ),
// Sends a chat message
sendChat: function(request) {
jQuery.ajax( {
url: url,
type: "GET",
dataType: "jsonp",
data: { 'msg' : request }
} );
},
// Toggle connection button
toggleConnect: function( button ) {
if( self.connection.hasConnection ) {
self.connection.disconnect();
jQuery( button ).val("Connect");
} else {
self.connection.connect();
jQuery( button ).val("Disconnect");
}
}
};
return self;
};
</script>
<script>
// Chat app initialisation
var chatAppContext = chatApp( 'http://paxjs.com/labs/longpolljsonp/chat.php' );
//var chatAppContext = chatApp( 'chat.php' );
var sendMessage = function() {
chatAppContext.sendChat( jQuery('#message').val() );
jQuery('#message').val('');
};
jQuery( '#connect' ).click( function() {
chatAppContext.toggleConnect( jQuery( this ).get(0) );
} );
jQuery( '#sendMsg' ).click( sendMessage );
jQuery( '#message').keyup(function(e) {
if(e.keyCode == 13)sendMessage();
});
</script>
</body>
</html>
External resources loaded into this fiddle: