pg.HttpNet = cc.Class.extend({ _http: null, _url: "http://192.168.199.88:8786" }); pg.HttpNet.create = function () { var res = new pg.HttpNet(); if (res && res.init()) { return res; } return null; }; pg.HttpNet.prototype.init = function () { return true; }; pg.HttpNet.prototype.send = function (msg) { try{ if(msg) { this.post(msg); }else { this.get(); } }catch (e){ }finally{ } }; pg.HttpNet.prototype.setUrl = function (url) { this._url = "http://" + url; }; pg.HttpNet.prototype.post = function (msg) { this._http = cc.loader.getXMLHttpRequest(); this._http.open("POST", this._url, true); this._http.responseType = "arraybuffer"; var that = this; this._http.onreadystatechange = function () { that.handler(that._http); }; this._http.send(msg); }; pg.HttpNet.prototype.get = function (url) { this._http = cc.loader.getXMLHttpRequest(); this._http.open("GET", this._url, true); this._http.responseType = "arraybuffer"; var that = this; this._http.onreadystatechange = function () { that.handler(that._http); }; this._http.send(); }; pg.HttpNet.prototype.handler = function (xhr) { if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) { if (xhr.response) { pg.HHandlerManager.getInstance().execute(xhr.response); } } };
var WebSocket = WebSocket || window.WebSocket || window.MozWebSocket; pg.SocketNet = cc.Class.extend({ state: WebSocket.CLOSED, _socket: null, _url: "ws://192.168.199.88:8780/websocket", _ip: "192.168.199.88", _port: "8780" }); pg.SocketNet.create = function () { var res = new pg.SocketNet(); if (res && res.init()) { return res; } return null; }; pg.SocketNet.prototype.init = function () { this._url = "ws://" + this._ip + ":" + this._port + "/websocket"; return true; }; pg.SocketNet.prototype.send = function (msg) { try { console.log("msg ==>" + msg.byteLength); this._socket.send(msg); } catch (e) { // pg.SceneManager.getInstance().gotoScene(pg.SceneManager.SceneType.LOGIN); } finally { } }; pg.SocketNet.prototype.setUrl = function (ip, port) { if (ip && port) { this._url = "ws://" + ip + ":" + port + "/websocket"; }else { this._url = "ws://" + ip + ":" + this._port + "/websocket"; } }; pg.SocketNet.prototype.connect = function (url) { if (url == undefined || url == null) { url = this._url; } cc.log("connet to url ==>" + url); this._socket = new WebSocket(url); this._socket.binaryType = "arraybuffer"; this.state = this._socket.CONNECTING; var that = this; this._socket.onopen = function (event) { cc.log("session onpen state ==>" + WebSocket.OPEN); that.state = WebSocket.OPEN; // CS_SessionMsg.create(WebSocket.OPEN); //TODO 发送进入房间消息 }; var that = this; this._socket.onmessage = function (event) { that.handlerData(event.data); }; this._socket.onclose = function () { console.log("socket closed==>"); }; }; pg.SocketNet.prototype.handlerData = function (data) { pg.SHandlerManager.getInstance().execute(data); }; pg.SocketNet.prototype.reconnect = function () { this.close(); this.connect(); }; pg.SocketNet.prototype.close = function () { cc.log("socket closed==>"); try { this._socket.close(); } catch (e) { } this._socket = null; this.state = WebSocket.CLOSED; };