調試中,馬馬虎虎能用
var LeesWebSocket = function (options) {
this.defaults = {
host: "127.0.0.1",
port: 2015,
// callbacks
onOpen: function () { },
onClose: function () { },
onMessage: function (evt) { },
onError: function () { }
}
//調試用---------------------------
mLCQ = new LeesCircleQueue(40);
function Trace(s) {
var t = new Date();
mLCQ.Push(t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds() + " :" + s + "<br>");
showDebuginfo();
}
function showDebuginfo() {
var ss = mLCQ.GetAllItem();
var sb = '';
for (var i = 0; i < ss.length; i++) {
sb += (i + 1) + ":" + ss[i] + "<br>";
}
$('#debug').html(sb);
}
//-------------------------------
//-------------------------------
var opt = $.extend({}, this.defaults, options);
var _this = this;
var wsSocket = null;
this.bIsConnected = false;
var bIsConnecting = false;
this._LWS_Host = "ws://" + opt.host + ":" + opt.port;
this.Connect = function () {
if (bIsConnecting)
return;
bIsConnecting = true;
try {
if ("WebSocket" in window) {
wsSocket = new WebSocket(this._LWS_Host);
}
else if ("MozWebSocket" in window) {
wsSocket = new MozWebSocket(this._LWS_Host);
}
wsSocket.onopen = WSonOpen;
wsSocket.onclose = WSonClose;
wsSocket.onmessage = WSonMessage;
wsSocket.onerror = WSonError;
} catch (ex) {
alert(ex);
}
bIsConnecting = false;
}
this.Send = function (s) {
Trace(s);
if (_this.bIsConnected) {
wsSocket.send(s);
return true;
}
return false;
}
this.IsConnected = function () {
return this.bIsConnected;
}
function WSonOpen() {
_this.bIsConnected = true;
opt.onOpen();
}
function WSonMessage(evt) {
opt.onMessage(evt);
}
function WSonClose() {
_this.bIsConnected = false;
wsSocket = null;
opt.onClose();
}
function WSonError(evt) {
_this.bIsConnected = false;
wsSocket = null;
opt.onError();
}
}