From 8aca667b868f0aee96ad2b08b77b9da06c61d61e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=BC=E5=A5=A5?= <1988wangxiao@gmail.com> Date: Fri, 23 Jan 2015 19:23:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[feature]=20=E4=BF=AE=E6=94=B9=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E5=90=AF=E5=8A=A8=E6=96=B9=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lc.push.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lc.push.js b/src/lc.push.js index 1af7036..2e29799 100644 --- a/src/lc.push.js +++ b/src/lc.push.js @@ -27,7 +27,7 @@ void function(win) { // 配置项 var config = { // 心跳时间(三分钟) - heartbeatsTime: 1 * 60 * 1000 + heartbeatsTime: 3 * 60 * 1000 }; // 命名空间,挂载一些工具方法 @@ -67,6 +67,8 @@ void function(win) { var wsOpen = function() { tool.log('WebSocket opened.'); engine.loginPush(cache.options); + // 启动心跳 + engine.heartbeats(); }; // WebSocket Close @@ -162,13 +164,11 @@ void function(win) { ws.addEventListener('close', wsClose); ws.addEventListener('message', wsMessage); ws.addEventListener('error', wsError); - - // 启动心跳 - engine.heartbeats(); }; // 心跳程序 engine.heartbeats = function() { + wsSend({}); cache.ws.addEventListener('message', function() { if (cache.heartbeatsTimer) { clearTimeout(cache.heartbeatsTimer); @@ -195,7 +195,7 @@ void function(win) { cmd: 'ack', appId: cache.options.appId, installationId: cache.options.id, - ids: idList + ids: idList }); }; @@ -203,7 +203,7 @@ void function(win) { wsSend({ cmd: 'login', appId: options.appId, - installationId: options.id + installationId: options.id }); }; @@ -378,7 +378,7 @@ void function(win) { tool.eventCenter = function() { var eventList = {}; var eventOnceList = {}; - + var _on = function(eventName, fun, isOnce) { if (!eventName) { tool.error('No event name.'); From cb4833ee4f633133a238d8b5c0222375a63b0f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=BC=E5=A5=A5?= <1988wangxiao@gmail.com> Date: Mon, 26 Jan 2015 16:18:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[feature]=20=E6=94=AF=E6=8C=81=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=B8=8D=E5=BC=80=E5=90=AF=E6=8E=A5=E6=94=B6=E6=8E=A8?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/test.js | 15 +++++------ src/lc.push.js | 69 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/demo/test.js b/demo/test.js index d34314b..2df829d 100644 --- a/demo/test.js +++ b/demo/test.js @@ -13,18 +13,17 @@ function createNew(callback) { appKey: appKey }); - push.on('open', function() { - callback({ - push: push - }); - }); - - push.on('close', function() { - console.log('close'); + push.open(function() { + console.log('可以接收推送'); }); push.on('message', function(data) { console.log('message'); console.log(data); }); + + push.send({ + channels: ['aaa'], + data: {wangxiao: 123} + }); } diff --git a/src/lc.push.js b/src/lc.push.js index 2e29799..9bc169e 100644 --- a/src/lc.push.js +++ b/src/lc.push.js @@ -69,6 +69,7 @@ void function(win) { engine.loginPush(cache.options); // 启动心跳 engine.heartbeats(); + cache.ec.emit(eNameIndex.open); }; // WebSocket Close @@ -93,7 +94,7 @@ void function(win) { }; var wsError = function(data) { - tool.error(data); + new Error(data); // TODO: 增加更加详细的错误处理 }; @@ -137,6 +138,7 @@ void function(win) { appId: options.appId, appKey: options.appKey, data: { + // TODO: 后续服务端要支持,改成 javascript deviceType: 'android', installationId: options.id, channels: options.channels @@ -154,6 +156,29 @@ void function(win) { }); }; + engine.sendPush = function(options, callback) { + tool.ajax({ + url: 'https://leancloud.cn/1.1/push', + method: 'post', + appId: options.appId, + appKey: options.appKey, + data: { + data: options.data + }, + channels: options.channels + }, function(data) { + if (data.lcError) { + setTimeout(function() { + engine.sendPush(options, callback); + }, 5000); + } else { + if (callback) { + callback(data); + } + } + }); + }; + engine.createSocket = function(server) { var ws = new WebSocket(server); cache.ws = ws; @@ -185,7 +210,7 @@ void function(win) { engine.createSocket(server.server); } else { - tool.error('WebSocket connet failed.'); + new Error('WebSocket connet failed.'); // TODO: 派发一个 Error 事件 } }; @@ -242,6 +267,7 @@ void function(win) { engine.connect({ server: cache.server }); + cache.ec.once(); } else { callback(tool.fail()); @@ -258,38 +284,42 @@ void function(win) { return this; }, on: function(eventName, callback) { - this.cache.ec.on(eventName, callback); + cache.ec.on(eventName, callback); return this; }, once: function(eventName, callback) { - this.cache.ec.once(eventName, callback); + cache.ec.once(eventName, callback); return this; }, emit: function(eventName, data) { - this.cache.ec.emit(eventName, data); + cache.ec.emit(eventName, data); + return this; + }, + send: function(options, callback) { + options.appId = cache.options.appId; + options.appKey = cache.options.appKey; + engine.sendPush(options, callback); return this; } }; }; // 主函数,启动通信并获得 pushObject - lc.push = function(options, callback) { + lc.push = function(options) { if (typeof options !== 'object') { - tool.error('lc.push need a argument at least.'); + new Error('lc.push need a argument at least.'); } else if (!options.appId) { - tool.error('Options must have appId.'); + new Error('Options must have appId.'); } else if (!options.appKey) { - tool.error('Options must have appKey.'); + new Error('Options must have appKey.'); } else { var pushObject = newPushObject(); options.id = engine.getId(options); pushObject.cache.options = options; pushObject.cache.ec = tool.eventCenter(); - // 启动 websocket - pushObject.open(callback); return pushObject; } }; @@ -304,7 +334,7 @@ void function(win) { // 空函数 tool.noop = function() {}; - // 获取一个唯一 id + // 获取一个唯一 id, 碰撞概率同一毫秒小于万分之一 tool.getId = function() { return 'lc' + (Date.now().toString(36) + Math.random().toString(36).substring(2, 3)); }; @@ -316,11 +346,6 @@ void function(win) { return obj; }; - // 输出错误信息 - tool.error = function(msg) { - throw new Error(msg); - }; - // 输出 log tool.log = function(msg) { console.log(msg); @@ -346,9 +371,7 @@ void function(win) { }; xhr.onerror = function() { callback(tool.fail()); - tool.error('Network error.'); - // TODO: 派发一个 Error 事件 - cache.ec.emit('error', {type:'network'}); + new Error('Network error.'); }; xhr.send(JSON.stringify(options.data)); }; @@ -381,10 +404,10 @@ void function(win) { var _on = function(eventName, fun, isOnce) { if (!eventName) { - tool.error('No event name.'); + new Error('No event name.'); } else if (!fun) { - tool.error('No callback function.'); + new Error('No callback function.'); } if (!isOnce) { @@ -410,7 +433,7 @@ void function(win) { }, emit: function(eventName, data) { if (!eventName) { - tool.error('No emit event name.'); + new Error('No emit event name.'); } var i = 0; var l = 0;