首页 > 编程语言 > 详细

原创:Javascript Websocket客户端封装

时间:2015-10-06 00:39:05      阅读:961      评论:0      收藏:0      [点我收藏+]
调试中,马马虎虎能用
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(); } }

 

原创:Javascript Websocket客户端封装

原文:http://www.cnblogs.com/xhzxlqt/p/4856554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!