實際上,我的感受,pomelo 目前提供的兩個默認sioconnector
和hybridconnector
使用的協議並不適合用於作手機推送平臺,在pomelo的一份公開ppt裏面,有提到過, 網易的消息推送平臺是基於pomelo開發的 (一個frontend 支持30w 長鏈接,消耗了3g 內存,若是我沒記錯數據應該是這樣),不過,這裏用的前端(frontend)實現的是基於MQTT協議,我估計這個基於MQTT協議實現的frontend,基本不可能開源出來.這裏只是說,默認提供的frontend不適合用於構建大型的推送平臺(c10m規模的),通常而言(c10k級別的),我的感受仍是夠用的.前端
爲了展現,更多pomelo 的相關特性,可能這裏的邏輯業務,與實際有所不一樣.敬請注意node
整個應用的架構圖:android
1 2 3 4 5 |
{ "role": "client/server", "apikey": "String", "clientId": "String" } |
發給web managementgit
1 2 3 4 5 |
{ "code": "Int httpCode ex: 200", "msg": "String", "users": "Array 客戶端的clientId 值 ex:["android1"] " } |
發給android客戶端程序員
1 2 3 4 |
{ "code": "Int httpCode ex: 200", "msg": "String" } |
android:github
connector route = sio-connector.entryHandler.enter, 用於把當前客戶端加入到推送頻道當中web
WebManagement:面試
connector route = hybrid-connector.entryHandler.enter,用於鏈接服務器.
backend route = pushserver.pushHandler.pushAll, 把消息推送到全部已鏈接的客戶端.後端
Pomelo 有個特色,就是約定開發,不少地方是約定好的配置,優勢是,架構清晰,可讀性好,缺點是,須要大量的文檔支持,目前而言,pomelo的官方文檔作的很差的地方就是,雖然文檔都有了,可是太零散了,分類不清楚,還有就是文檔沒跟上開發,有時候,你不閱讀裏面源碼根本不知道這個api要傳那些參數.api
因爲pomelo 0.3 之後新增了一個新的connector:hybridconnector,支持socket和websocket,使用二進制通信協議,可是除了,網頁js版本和c 客戶端實現了這個connector,其餘客戶端均還沒實現,因此,咱們還須要一個兼容android 客戶端的connector: siocnnector,關於兩個connector 具體比較,之後有空重寫這篇的時候,暫時,你只要知道,這個兩個connector,一個基於socket.io,一個基於socket和websocket 便可.
app.js 因爲咱們用到了兩個不一樣的connector,因此要在app.js寫上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// 支持 socket.io app.configure('production|development', 'sio-connector', function(){ app.set('connectorConfig', { connector : pomelo.connectors.sioconnector }); }); //支持 websocket 和 socket app.configure('production|development', 'hybrid-connector', function(){ app.set('connectorConfig', { connector : pomelo.connectors.hybridconnector, heartbeat : 300, useDict: true, useProtobuf: true }); }); |
通過這樣的配置,咱們就可以使用兩個不一樣的connector了.
用pomelo 進行消息的推送,很是便捷,因爲,咱們如今只關注推消息給所有客戶端,那樣就很是簡單了.
推送流程:
爲了教學的方便,這裏的uuid 硬編碼爲: xxx-xx--xx-xx
把客戶端添加到相應的channel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//把客戶端添加到推送列表中 PushRemote.prototype.add = function(uid, role, sid, channelName, cb){ var channel = this.channelService.getChannel(channelName, true); if(role === 'server'){ //web 服務端直接返回用戶列表 cb(null ,this.getUsers(channelName)); }else { if(!!channel){ channel.add(uid ,sid); } //uuid 告訴給服務端onAdd 事件 // [{uid: userId, sid: frontendServerId}] var server = [{uid: channelName, sid: sid}]; this.channelService.pushMessageByUids('onAdd', {msg: "add ok", users:this.getUsers(channelName)},server, function(err){ if(err){ console.log(err); return; } }); } }; |
Frontend 利用rpc 調用pushserver 添加客戶端到相應頻道的方法.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//sid 統一爲web managment 所在的 frontend server. this.app.rpc.pushserver.pushRemote.add(session, uid,role, 'connector-server-client', uuid, function(err, users){ if(err){ console.log(err); return; } if(users){ next(null, {code: 200, msg: 'push server is ok.', users: users}); }else{ next(null,{code: 200, msg: "add ok", users: users}); } }); |
web 管理端調用消息推送
1 2 3 4 5 6 7 8 9 10 11 12 |
Handler.prototype.pushAll = function(msg, session, next){ var pushMsg = this.channelService.getChannel(msg.apikey, false); pushMsg.pushMessage('onMsg',{msg: msg.msg}, function(err){ if(err){ console.log(err); } else{ console.log('push ok'); next(null, {code: 200, msg: 'push is ok.'}); } }); }; |
以上就是主要客戶端如何加入到推送隊列的代碼,以及web 管理端進行消息推送的主要代碼,是否是很簡單! 完整代碼能夠參閱個人github https://github.com/youxiachai
有一點要注意的,若是pomelo 項目要部署到外網或者局域網,frontend 的host 要填寫當前host 主機的ip 地址
例如:
1 2 3 |
"connector": [ {"id": "connector-server-1", "host": "127.0.0.1", "port": 3150, "clientPort": 3010, "frontend": true} ] |
部署到某臺服務器,須要修改
1 2 3 |
"connector": [ {"id": "connector-server-1", "host": "192.168.1.107", "port": 3150, "clientPort": 3010, "frontend": true} ] |
客戶端訪問相應的host 的地址.
客戶端和服務端的github 地址: https://github.com/youxiachai/pomelo-pushServer-Demo
若是,你如今對pomelo感興趣的話,你能夠看下我寫的pomelo 的系列教程(由於還沒寫好因此暫時只發布在個人博客)暫時一共四篇.基本涵蓋了pomelo 大部分基本知識點.
http://blog.gfdsa.net/tags/pomelo/
廣州有招nodejs 程序員(有兩年android 開發經驗..orz)的嗎...可否給個面試機會,聯繫郵箱: youxiachai@gmail.com
參與的相關社區:
github: https://github.com/youxiachai
cnodejs(Top積分榜 14 ...): http://cnodejs.org/user/youxiachai