jQuery Long Poll plugin 0.0.1
Plugin jQuery para el uso de longpolling
HTML
<script src="http://jquery-jsonp.googlecode.com/svn/trunk/core/jquery.jsonp.js"></script>
<!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 {
...
JavaScript
/*
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: '148.251.4.172', // 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) {
...