完善pomelo客户端

This commit is contained in:
leo
2018-08-22 10:57:12 +08:00
parent cd8aa7d6c5
commit 821a8f550d
47 changed files with 4468 additions and 322 deletions

View File

@@ -1,3 +1,8 @@
console.log('Globals');
require('PomeloClient');
console.log('pomelo', window.pomelo);
window.Promise = require('promise');
let myModel = {};
window.myModel = myModel;
myModel.chatUserModel = require('ChatUserModel');
myModel.chatInfoModel = require('ChatInfoModel');
// console.log('promise=', window.Promise);

View File

@@ -1,75 +0,0 @@
cc.Class({
extends: cc.Component,
properties: {
label: {
default: null,
type: cc.Label
},
// defaults, set visually when attaching this script to the Canvas
text: 'Hello, World!'
},
// use this for initialization
onLoad: function () {
this.label.string = this.text;
this.connectServer({
host: '127.0.0.1',
uid: '' + Date.now(),
port: 3014,
log: true
}, (err) => {
console.log('err');
}, (data) => {
console.log('gate', data);
this.label.string = data.host + ':' + data.port
});
console.log(Date.now());
},
/**
*
*/
connectServer(data, showError, callback) {
console.log('data');
var route = 'gate.gateHandler.queryEntry';
pomelo.init({
host: data.host,
// reconnect: true,
port: 3014,
log: true
}, function () {
pomelo.request(route, {
uid: data.uid
}, function (data) {
pomelo.disconnect();
if (data.code === 500) {
showError(LOGIN_ERROR);
return;
}
callback(data);
});
});
// pomelo.init({
// host: data.host,
// port: data.port
// }, function () {
// var route = 'gate.gateHandler.queryEntry';
// pomelo.request(route, {}, function () {
// pomelo.disconnect(function () {
// pomelo.init({
// host: host2,
// port: port2,
// reconnect: true
// }, function () {})
// });
// })
// });
},
// called every frame
update: function (dt) {
},
});

View File

@@ -1344,6 +1344,7 @@ cc.Pomelo = function () {
pomelo.emit('close', event);
pomelo.emit('disconnect', event);
console.log('socket close: ', event);
// params.onclose && params.onclose();
if (!!params.reconnect && reconnectAttempts < maxReconnectAttempts) {
reconnect = true;
reconnectAttempts++;

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"ver": "1.0.5",
"uuid": "280c3aec-6492-4a9d-9f51-a9b00b570b4a",
"uuid": "1b74425f-4146-4ab5-be70-87c947e60228",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,

View File

@@ -0,0 +1,5 @@
{
"ver": "1.0.1",
"uuid": "0c50863e-702d-445a-bf94-b0aafef89992",
"subMetas": {}
}

View File

@@ -0,0 +1,23 @@
class ChatInfoModel {
constructor() {
this.chatInfo = {};
}
setName(name) {
this.chatInfo.name = name;
}
getName() {
return this.chatInfo.name;
}
setRoomId(rid) {
this.chatInfo.rid = rid;
}
getRoomId() {
return this.chatInfo.rid;
}
}
module.exports = new ChatInfoModel();

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "756b8874-c003-4da3-be5f-51d7f0f6ad5e",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,20 @@
class ChatUserModel {
constructor() {
this.users = [];
}
setUsers(arr) {
delete this.users;
this.users = arr;
}
getUser() {
return this.users;
}
addUser(user) {
this.users.push(user);
}
}
module.exports = new ChatUserModel();

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "e6a0f93a-9079-44dc-b204-ce03cdf9764e",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,5 @@
{
"ver": "1.0.1",
"uuid": "c8fc2d5d-105e-49d1-8e36-858e1a45714f",
"subMetas": {}
}

View File

@@ -0,0 +1,81 @@
class ChatRoomApi {
constructor() {
}
init(opt) {
this.opt = opt;
this.chatInfo = myModel.chatInfoModel;
this.listenEvents();
}
bindEvent(event, context) {
return event.bind(context);
}
listenEvents() {
this.bindOnDisconnect = this.bindEvent(this.onDisconnect, this);
this.bindOnChat = this.bindEvent(this.onChat, this);
this.bindOnAdd = this.bindEvent(this.onAdd, this);
this.bindOnLeave = this.bindEvent(this.onLeave, this);
pomelo.on('disconnect', this.bindOnDisconnect);
pomelo.on('onChat', this.bindOnChat);
pomelo.on('onAdd', this.bindOnAdd);
pomelo.on('onLeave', this.bindOnLeave);
}
onDisconnect() {
console.log('disconnect data');
this.opt.onDisconnect && this.opt.onDisconnect();
}
onChat(data) {
console.log('onChat data', data);
this.opt.onChat && this.opt.onChat(data);
}
onAdd(data) {
console.log('onAdd data=', data);
this.opt.onAdd && this.opt.onAdd(data);
}
onLeave(data) {
console.log('onLeave data=', data);
this.opt.onLeave && this.opt.onLeave(data);
}
sendMsg(msg) {
let route = "chat.chatHandler.send";
return new Promise((resolve, reject) => {
try {
pomelo.request(route, {
rid: this.chatInfo.getRoomId(),
content: msg,
from: this.chatInfo.getName(),
target: '*'
}, function (data) {
console.log('sendMsg ', data);
resolve(data);
});
} catch (error) {
reject(error);
}
});
}
removeEvents() {
pomelo.off('disconnect', this.bindOnDisconnect);
pomelo.off('onChat', this.bindOnChat);
pomelo.off('onAdd', this.bindOnAdd);
pomelo.off('onLeave', this.bindOnLeave);
}
destroy() {
this.removeEvents();
this.opt = {};
this.chatInfo = {};
}
}
module.exports = new ChatRoomApi();

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "49b55556-d2f5-4419-8c98-938a892c76e3",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,60 @@
const LOGIN_ERROR = "There is no server to log in, please wait.";
const DUPLICATE_ERROR = "Please change your name to login.";
class LoginApi {
constructor() {
this.host = '192.168.0.124'
this.port = 3014;
}
// query connector
queryEntry(uid) {
let route = 'gate.gateHandler.queryEntry';
return new Promise((resolve, reject) => {
pomelo.init({
host: this.host,
port: this.port,
log: true
}, function () {
pomelo.request(route, {
uid: uid
}, function (data) {
pomelo.disconnect(function () {
console.log(this, data);
if (data.code === 500) {
reject(LOGIN_ERROR);
return;
}
resolve(data);
});
});
});
});
};
// connect entry
loginEntry(data, user) {
var route = "connector.entryHandler.enter";
return new Promise((resolve, reject) => {
pomelo.init({
host: data.host,
port: data.port,
log: true
}, function () {
pomelo.request(route, {
username: user.username,
rid: user.rid
}, function (data) {
if (data.error) {
reject(DUPLICATE_ERROR);
return;
}
resolve(data);
});
});
});
};
}
module.exports = new LoginApi();

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "2170af5c-4855-488f-92ed-a9af0f7fb84c",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,5 @@
{
"ver": "1.0.1",
"uuid": "ad3d0321-a18c-48df-9864-34633c2d46aa",
"subMetas": {}
}

View File

@@ -0,0 +1,5 @@
{
"ver": "1.0.1",
"uuid": "33659f56-7756-4b3d-b02d-aa1deff9729e",
"subMetas": {}
}

View File

@@ -0,0 +1,26 @@
const timeUtil = require('TimeUtil');
cc.Class({
extends: cc.Component,
properties: {
timeLabel: cc.Label,
nameLabel: cc.Label,
contextLabel: cc.Label
},
onLoad() {
},
init(data) {
this.contextLabel.node.active = false;
this.timeLabel.string = timeUtil.timeString(new Date());
this.nameLabel.string = data.name + ' : ' + data.msg;
}
// start () {
// },
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "4fde3bae-aba9-4942-9679-3a8c5ac97448",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,28 @@
cc.Class({
extends: cc.Component,
properties: {
nodeContent: cc.Node,
prefabItem: cc.Prefab,
editBoxInput: cc.EditBox
},
onLoad() {
this.nodeContent.removeAllChildren();
},
createItem(data, componentName) {
let node = cc.instantiate(this.prefabItem);
node.script = node.getComponent(componentName);
node.script.init();
this.nodeContent.addChild(node);
},
onEventClicked_send() {
if (!this.editBoxInput.string) {
return;
}
}
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "022bc4ef-5334-4a19-a38b-0238660ddabc",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,22 @@
cc.Class({
extends: cc.Component,
properties: {
labelName: cc.Label
},
onLoad() {
},
init(name) {
this.labelName.string = name;
}
// start () {
// },
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "5ee70e85-f69b-4fcf-b209-e9d12a0ebeea",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,14 @@
cc.Class({
extends: cc.Component,
properties: {
nodeContext: cc.Node
},
onLoad() {
this.nodeContext.removeAllChildren();
},
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "2092b3d2-067d-47f5-93f0-033a06008068",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,5 @@
{
"ver": "1.0.1",
"uuid": "be92e8c5-4737-4f45-a958-7afcca0015c5",
"subMetas": {}
}

View File

@@ -0,0 +1,44 @@
cc.Class({
extends: cc.Component,
properties: {
nodeContent: cc.Node,
prefabItem: cc.Prefab,
itemName: ''
},
// onLoad() {
// },
init(args = {}) {
this.clear();
let data = args.data || [];
let len = data.length;
for (let i = 0; i < len; i++) {
this.createItem({
data: data[i],
fn: args.fn
});
}
},
createItem(args) {
let data = args.data
let node = cc.instantiate(this.prefabItem);
node.script = node.getComponent(this.itemName);
this.nodeContent.addChild(node);
node.script.init(data);
args.fn && args.fn(node.script)
},
clear() {
this.nodeContent.removeAllChildren();
},
childrenCount() {
return this.nodeContent.childrenCount;
}
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "b2a96011-d6e0-447f-b6b9-1dd3bbb3df35",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,5 @@
{
"ver": "1.0.1",
"uuid": "48dfcc38-233e-499e-9681-7a182fa0c907",
"subMetas": {}
}

View File

@@ -0,0 +1,69 @@
const chatRoomApi = require('ChatRoomApi');
cc.Class({
extends: cc.Component,
properties: {
nodeScrollViewChat: cc.Node,
nodeScrollViewUser: cc.Node,
editBoxInput: cc.EditBox
},
onLoad() {
console.log('ChatRoomApi');
this.nodeScrollViewChatScript = this.nodeScrollViewChat.getComponent('ScrollViewMgr');
this.nodeScrollViewUserScript = this.nodeScrollViewUser.getComponent('ScrollViewMgr');
console.log(this.chatUiScript);
this.nodeScrollViewChatScript.init();
this.nodeScrollViewUserScript.init({
data: myModel.chatUserModel.getUser()
});
let opts = {};
opts.onAdd = this.onAdd.bind(this);
opts.onChat = this.onChat.bind(this);
chatRoomApi.init(opts);
},
onDisconnect() {
console.log('disconnect data');
},
onChat(data) {
console.log(this);
console.log('onChat data', data);
this.nodeScrollViewChatScript.createItem({
data: {
name: data.from,
msg: data.msg
}
});
},
onAdd(data) {
console.log('onAdd data=', data);
},
onLeave(data) {
console.log('onLeave data=', data);
},
onEventClicked_send() {
let msg = this.editBoxInput.string || '';
if (!msg) {
return;
}
console.log('msg=', msg);
chatRoomApi.sendMsg(msg).then(() => {
this.editBoxInput.string = '';
}).catch((err) => {
console.log('err', err);
});
},
onDestroy() {
this.chatRoomApi.destroy();
}
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "a7c42b43-b7e9-49ef-9418-22a527429647",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,49 @@
const loginApi = require('LoginApi');
cc.Class({
extends: cc.Component,
properties: {
editBoxID: cc.EditBox,
editBoxRoom: cc.EditBox,
},
onLoad() {
},
/**
*
*/
onEventClicked_login() {
console.log('onEventClicked_login');
let id = this.editBoxID.string || Date.now();
let room = this.editBoxRoom.string || 'room_1';
//
loginApi.queryEntry(id).then((data) => {
return loginApi.loginEntry(data, {
username: id,
rid: room
});
}).then((data) => {
myModel.chatInfoModel.setName(id);
myModel.chatInfoModel.setRoomId(room);
this.onLogin(data);
}).catch((err) => {
console.log('err=', err);
});
},
/**
*
* @param {*} data
*/
onLogin(data) {
console.log('login data', data);
myModel.chatUserModel.setUsers(data.users);
cc.director.loadScene('Chat');
}
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "403797af-36fa-4bac-8ef1-4ede1c05cd01",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,5 @@
{
"ver": "1.0.1",
"uuid": "31cc75cb-0083-4372-90fd-3f5df0297410",
"subMetas": {}
}

View File

@@ -0,0 +1,25 @@
function TimeUtil() {
console.log('TimeUtil');
}
//pads n with zeros on the left,
//digits is minimum length of output
//zeroPad(3, 5); returns "005"
//zeroPad(2, 500); returns "500"
function zeroPad(digits, n) {
n = n.toString();
while (n.length < digits)
n = '0' + n;
return n;
};
//it is almost 8 o'clock PM here
//timeString(new Date); returns "19:49"
TimeUtil.prototype.timeString = function (date) {
console.log('timeString');
var minutes = date.getMinutes().toString();
var hours = date.getHours().toString();
return zeroPad(2, hours) + ":" + zeroPad(2, minutes);
};
module.exports = new TimeUtil();

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "04425e17-a421-408e-8891-81835fce9781",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}