| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // var qqgamesdk = require('qqgamesdk');
- var qqgamesdk = require('qqgame-sdk-0.3.6.min');
- window.qqNetwork = {};
- // load sdk
- qqNetwork.qqSdkInitFlag = false;
- if (!cc.sys.isNative) {
- }
- qqNetwork.room2Game = {
- channelCreated:"channelCreated", // 通道创建,大厅到游戏
- gameData:"gameData", // 数据传输,游戏到大厅或者大厅到游戏
- frameData:"frameData",
- gameStart:"gameStart", // 游戏开始,大厅到游戏
- network:"network",
- leave:"leave",
- voice:"voice",
- gameReset:"gameReset" // 游戏重置,大厅到游戏
- };
- qqNetwork.game2Room = {
- gameInit:"gameInit", // 游戏初始化,游戏到大厅
- gameData:"gameData", // 游戏数据传输,游戏到大厅或者大厅到游戏
- gameReady:"gameReady", // 游戏ready,游戏到大厅
- gameResult:"gameResult", //结算信息,游戏到大厅
- frameData:"frameData",
- leave:"leave" // 退出,游戏到大厅
- };
- qqNetwork.msgProcess = function(msgName, msgData) {
- if (typeof msgData === "string") {
- if (msgData != "gameStart") {
- msgData = JSON.parse(msgData);
- }
- }
- switch (msgName) {
- case qqNetwork.room2Game.channelCreated:
- console.log('channelCreated')
- var paramFlag1 = false;
- var paramFlag2 = false;
- if (msgData.playerInfos[0].headurl && msgData.playerInfos[0].headurl.indexOf("?") !== -1) {
- paramFlag1 = true;
- }
- if (msgData.playerInfos[1].headurl && msgData.playerInfos[1].headurl.indexOf("?") !== -1) {
- paramFlag2 = true;
- }
- msgData.playerInfos[0].headurl = decodeURI(msgData.playerInfos[0].headurl);
- msgData.playerInfos[1].headurl = decodeURI(msgData.playerInfos[1].headurl);
- if (paramFlag1) {
- msgData.playerInfos[0].headurl = msgData.playerInfos[0].headurl + "&aa=aa.jpg";
- } else {
- msgData.playerInfos[0].headurl = msgData.playerInfos[0].headurl + "?aa=aa.jpg";
- }
- if (paramFlag2) {
- msgData.playerInfos[1].headurl = msgData.playerInfos[1].headurl + "&aa=aa.jpg";
- } else {
- msgData.playerInfos[1].headurl = msgData.playerInfos[1].headurl + "?aa=aa.jpg";
- }
- qqNetwork.send(qqNetwork.game2Room.gameReady, {});
- break;
- }
- if (this.msgCb) this.msgCb(msgName, msgData);
- };
- qqNetwork._init = function() {
- this.qqSdkInitFlag = true;
- if (this.debugFlag) {
- return;
- }
- console.log('EnterInitFunction');
- if (window.qqgame) {
- console.log('QQ Init');
- window.qqgame.onMessage(function(msgName, msgData) {
- this.msgProcess(msgName, msgData)
- }.bind(this));
- }
- };
- qqNetwork.connect = function(ip, port, cb) {
- port = port || 8181;
- // 测试用,正在大厅用不上
- console.log('Socket Connet');
- var socketUrl = port !== 0 ? "ws://" + ip + ":" + port: "ws://" + ip;
- if (this.web_socket) {
- this.web_socket.close();
- this.web_socket = null;
- }
- this.web_socket = new WebSocket(socketUrl);
- // this.web_socket.binaryType = "arraybuffer";
- this.web_socket.onmessage = function (event) {
- var data = JSON.parse(event.data);
- var msgName = data["eID"];
- var msgData = data["ePara"];
- this.msgProcess(msgName, msgData);
- // console.log("receive data:" + event.data);
- }.bind(this);
- this.web_socket.onopen = function (event) {
- cc.log("onopen------------");
- if (cb) cb();
- if (this.msgCb) this.msgCb("online", {});
- }.bind(this);
- this.web_socket.onclose = function (event) {
- cc.log("onclose------------");
- this.web_socket = null;
- if (this.msgCb) this.msgCb("offline", {});
- }.bind(this);
- this.web_socket.onerror = function (event) {
- cc.log("onerror------------");
- if (this.msgCb) this.msgCb("offline", {});
- }.bind(this);
- };
- qqNetwork.send = function(reqName, originData) {
- var data = {
- "eID":reqName,
- "ePara":originData,
- };
- // console.log("send data:" + JSON.stringify(data));
- if (!this.debugFlag) {
- if (this.qqSdkInitFlag) {
- window.qqgame.invoke(reqName, originData);
- } else {
- setTimeout(function() {
- this.send(reqName, originData);
- }.bind(this), 200);
- }
- } else {
- if (!this.web_socket) {
- console.error("debug mode need connect websocket");
- return;
- }
- this.web_socket.send(JSON.stringify(data));
- }
- };
- qqNetwork.enableDebug = function() {
- this.debugFlag = false;
- console.log('debugFlag=='+this.debugFlag);
- };
- qqNetwork.regMsgCallback = function(cb) {
- qqNetwork.msgCb = cb;
- };
- qqNetwork._init();
- window.network = qqNetwork;
|