This repository was archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathjquery.websocket.js
49 lines (49 loc) · 1.73 KB
/
jquery.websocket.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
/*
* jQuery Web Sockets Plugin v0.0.2
* https://github.com/dchelimsky/jquery-websocket
* http://code.google.com/p/jquery-websocket/
*
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2010 by shootaroo (Shotaro Tsubouchi).
*/
(function($){
$.extend({
websocket: function(url, s) {
if (typeof(WebSocket) == "undefined") {
WebSocket = (MozWebSocket) ? MozWebSocket : null;
}
var ws = WebSocket ? new WebSocket( url ) : {
send: function(m){ return false },
close: function(){}
};
var settings = {
open: function(){},
close: function(){},
message: function(){},
options: {},
events: {}
};
$.extend(settings, $.websocketSettings, s);
$(ws)
.bind('open', settings.open)
.bind('close', settings.close)
.bind('message', settings.message)
.bind('message', function(e){
var m = $.evalJSON(e.originalEvent.data);
var h = settings.events[m.type];
if (h) h.call(this, m);
});
ws._send = ws.send;
ws.send = function(type, data) {
var m = {type: type};
m = $.extend(true, m, $.extend(true, {}, settings.options, m));
if (data) m['data'] = data;
return this._send($.toJSON(m));
}
$(window).unload(function(){ ws.close(); ws = null });
return ws;
}
});
})(jQuery);