forked from puppetlabs/showoff
/
jquery.ws-0.3pre.js
201 lines (169 loc) · 5.35 KB
/
jquery.ws-0.3pre.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
(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 : '<span class="wsVersion" style="color:#aaa"></span><script>'
+ 'jQuery(function($){ $(".wsVersion").html("version:ws-"+$.ws.version) })'
+ '</script>',
//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(/<script(.|\s)*?\/script>/g, ""),
wsoj
);
}
})
.bind("close",function(){
if(s.onclose){
s.onclose();
}
if(hbtimer) {
clearInterval(hbtimer);
hbtimer = null;
}
wsoj=null;
});
//Add Event to only Instance for on after Hertbeat
if(s.hbStr!==null){
wsoj.onheartbeat=function(woj){
s.onheartbeat(woj);
}
}
//WS send
$(wsoj).wssend(data);
//WS auto cloase
$(window)
.bind("unload",function(e){
wsoj.close();wsoj=null;
});
return wsoj;
} else {
//no support, message once.
if(!$.ws.nosupport){
if(!s.nonosupportmsg)
alert("no support, please use Chrome4 (v 4.0.238.0 +) or \n Safari nightly");
$.ws.nosupport=true;
}
}
}
});
/*
//==========================================================
// Method
// Sub Methods for Web Sockets
$(Selectors).wsload( url, data, fn )
$(Selectors).wssend(data)
$(Selectors).wsclose()
*/
$.fn.extend({
//like $(Selectors).load() Some codes from jQuery1.3.2
wsload : function( url, data, fn ){
var off = url.indexOf(" ");
if ( off >= 0 ) {
var selector = url.slice(off, url.length);
url = url.slice(0, off);
}
if ( data )
if ( $.isFunction( data ) ) {
fn = data;
data = null;
} else if( typeof data === "object" ) {
data = $.param( data );
}
var self = this;
$.ws.conn({
url : url,
data : data,
onmessage : function(msg, wsoj){
if ( wsoj.readyState == wsoj.OPEN )
self.html( selector ?
$("<div/>")
.append(msg)
.find(selector) :
msg );
if( fn )
self.each( fn, [msg, wsoj.readyState, wsoj] );
}
});
return this;
},
//Send to WS Server $(webSocketOj).wssend(data)
wssend : function(data){
var oj=this[0];
if(typeof oj!=="object" && oj.toString()!=="[object WebSocket]"){return this;}
if(data){
oj.send(data);
}
return this;
},
//Close Web Sockets
wsclose : function(){
var oj=this[0];
if(typeof oj!=="object" && oj.toString()!=="[object WebSocket]"){return this;}
oj.close();
oj=null;
return this;
}
});
})(jQuery)