(function($){
$.ws = {
//==========================================================
// jQuery plugin jquery.ws.js
// for Web Sockets
// need Browser Chrome4.0.249.0+
// Demo http://bloga.jp/ws/jq/
// serverside sample @see http://blog.livedoor.jp/kotesaki/archives/1355651.html
name : "ws",
version : "0.3-noenc-pre",
demo : "http://bloga.jp/ws/jq/conn/b1.htm",
author : "Toshiro Takahashi",
lisence : "same as jQuery @see http://docs.jquery.com/Licensing",
update : 'http://jsgt.org/lib/jquery/plugin/ws/update.txt',
ver : '',
//Default settings
wsSettings: {
url : "ws://"+location.host,
data : null,//The data which transmit a message
onopen : function(e){},//callback on opened.
onmessage : function(msg,wsObject){},//callback on received
onclose : function(){},//callback on cloased
hbStr : "Heartbeat",//if null then no Heartbeat
hbinterval : 60000,//dafault 60sec, min=5000
onheartbeat: function(){}//callback on heartbeatsended
},
wsSetup: function( settings ) {
jQuery.extend( jQuery.ws.wsSettings, settings );
}
//Note: if you want to stop no-support alert dialog,
//$.ws.wsSetup({nonosupportmsg:true});
};
/*
//==========================================================
// Method
// Basic Method of this pulgin for Web Sockets
$.conn( settings )
*/
$.extend($.ws ,{
conn : function( s ){
s = $.extend(true, s, $.extend(true, {}, $.ws.wsSettings, s));
if ("WebSocket" in window) {
var url=s.url ,
//WS Object
wsoj = new WebSocket( url ) ,
data = s.data ,
//Heartbeat
_MIN_HBINTERVAL=5000,
_INI_HBINTERVAL=60000,
hbtimer=null,
hbStr = (s.hbStr===null)?null:(typeof s.hbStr==='string')?s.hbStr:'Heartbeat',
hbinterval = (typeof s.hbinterval==='number')?
(s.hbinterval>=_MIN_HBINTERVAL)?s.hbinterval:_INI_HBINTERVAL
:_INI_HBINTERVAL;
//WS Events bind
$(wsoj)
.bind("open",function(e){
if(s.onopen){ s.onopen(e); } ;
if(s.hbStr!==null){
hbtimer = setInterval(function(){
$(wsoj).wssend(hbStr);
if(wsoj.onheartbeat){wsoj.onheartbeat(wsoj)}
}, hbinterval);
}
})
.bind("message",function(e){
if(s.onmessage){
s.onmessage(
e.originalEvent.data
.replace(/