寫在前面
這幾天由於公司需要自己開發一個手機APP的推送,經過一番查找發現有pomelo這么一個框架,在做了一定了解后,決定先搭一個測試環境試一試。
先貼一個Pomelo在github上的地址https://github.com/NetEase/pomelo/wiki/Home-in-Chinese
關於Pomelo開發環境的搭建我在這就不復述了,在《pomelo快速使用指南》中有很詳細的描述,或者參考《pomelo windows 環境下開發環境搭建》。
開發環境搭建完成之后,就開始我的學習之旅了!
關於Pomelo API
在使用框架的時候,對它提供的API必須要做詳細的了解。
Pomelo API總共分為以下幾類:
環境
API | 說明 |
getBase() | Application.getBase(); 獲取應用程序的基本路徑 |
set(setting, val, attach); |
Application.set(); setting:應用程序的配置;val:需要設置的值;attach:是否將設配置應用到程序。設置或返回配置的值。 |
get(setting) | Application.get(); setting:應用程序的配置。獲取配置的值 |
enabled(setting) | Application.enabled(); setting:應用程序的配置。檢查配置是否啟用 |
disabled(setting) | Application.disabled(); setting:應用程序的配置。檢查配置是否禁用 |
enbale(setting) | Application.enbale(); setting:應用程序的配置。啟用配置 |
disable(setting) | Application.disabled(); setting:應用程序的配置。禁用配置 |
configure(env,fn,type) | Application.configure();env:應用環境;fn:回調函數;type:服務類型. |
初始化
API | 說明 |
start() | Application.start(); 啟動應用程序。它會加載默認的組件和啟動所有加載的組件。 |
registerAdmin(moduleId,module,opts) | Application.registerAdmin(); moduleId:(可選參數)模塊id或者有modeule提供的模塊Id;module:模塊對象或者模塊的工程函數;opts:模塊構造函數的參數。 |
filter(filter) | Application.filter(); filter:provide before and after filter method。add a filter to before and after filter |
before(bf) | Application.before(); bf:before filter。Add before filter |
after(af) | Application.after(); af:after filter。Add after filter |
load(name, component, opts) | Application.load(); name:組件的名稱(可選);component:組件的實例或者組件的工廠函數;opts:組件構造函數的參數(可選)。加載組件 |
loadConfig(key,val) | Application.loadConfig(); key:環境配置的關鍵字;val:環境配置的值。導入json文件來配置環境。 |
組件相關
API | 說明 |
route(serverType, routeFunc) | Application.route(); serverType:服務類型;routeFunc:路由功能函數,如:routeFunc(session, msg, app, cb) 未指定的服務類型設置路由功能。如: app.route('area', routeFunc); var routeFunc = function(session, msg, app, cb) { // all request to area would be route to the first area server var areas = app.getServersByType('area'); cb(null, areas[0].id); } |
獲取相關配置,組件方法
API | 說明 |
getMaster() | Application.getMaster() 獲得Maseter服務的信息 |
getCurServer() | Application.getCurServer() 獲得當前服務的信息 |
getServerId() | Application.getServerId() 獲得當前服務的ID |
getServerType() | Application.getServerType() 獲得當前服務的類型 |
getServers() | Application.getServers() 獲得所有當前服務的信息 |
getServersFromConfig() | Application.getServersFromConfig() 從server.json中獲得所有服務的信息 |
getServerTypes() | Application.getServerTypes() 獲得所有服務的類型 |
getServerById(serverId) | Application.getServerById() 根據服務ID從服務集群中獲得服務的信息 |
getServerFromConfig(serverId) | Application.getServerFromConfig() 根據服務ID從server.json中獲得服務的信息 |
getServersByType(serverType) | Application.getServersByType() 根據服務類型獲取服務信息 |
isFrontend(server) | Application.isFrontend() 檢查服務是否是一個前端服務 |
isBackend(server) | Application.isBackend() 檢查服務是否是一個后端服務 |
isMaster() | Application.isMaster() 檢查當前服務是否是主服務 |
addServers(servers) | Application.addServers() servers:新服務信息列表。添加新服務信息到正在運行的應用程序中 |
removerServers(ids) | Application.removerServers() ids:服務id列表。從當前運行的應用程序中刪除服務信息。 |
創建和維護本地服務的信道。
API | 說明 |
createChannel(name) | ChannelService.prototype.createChannel() 根據信道名稱創建信道,如果該信道已存在則返回已存在的信道 |
getChannel(name,create) | ChannelService.prototype.getChannel() name:信道名稱,create:如果為true,並且信道不存在時,則創建新的信道。根據信道名稱獲取信道 |
destroyChannel(name) | ChannelService.prototype.destroyChannel() 根據信道名稱,刪除信道 |
pushMessageByUids(route, msg, uids, cb) | ChannelService.prototype.pushMessageByUids() route:消息路由;msg:發送到客戶端的消息;uids:接收消息的客戶端列表,格式 [{uid: userId, sid: frontendServerId}];cb:回調函數 cb(err)。根據uids將消息推送給客戶端,如果uids中的sid未指定,則忽略相應的客戶端 |
broadcast(stype,route, msg, opts, cb) | ChannelService.prototype.broadcast() stype:前端服務的類型;route:路由;msg:消息;opts:廣播參數;cb:回調函數。廣播消息到所有連接的客戶端。 |
API | 說明 |
add(uid,sid) | Channel.prototype.add() uid:用戶編號;sid:用戶連接到的前端服務id。添加指定用戶到信道。 |
leave(uid,sid) | Channel.prototype.leave() uid:用戶編號;sid:用戶連接到的前端服務id。從信道中移除用戶。 |
getMembers() | Channel.prototype.getMembers() 獲得信道中的成員 |
getMember(uid) | Channel.prototype.getMember() 根據uid獲取成員信息 |
destroy() | Channel.prototype.destroy() 銷毀信道 |
pushMessage(route,msg,cb) | Channel.prototype.pushMessage() route:消息路由,msg:要推送的消息,cb:回調函數。將消息推送給信道的所有成員。 |
API | 說明 |
destroyChannel(name,cb) | GlobalChannelService.prototype.destroyChannel() uid:用戶編號;sid:用戶連接到的前端服務id。添加指定用戶到信道。 |
add(name,uid,sid,cb) | GlobalChannelService.prototype.add() name:信道名稱;uid:用戶id;sid:前端服務id;cb:回調函數。 添加成員到信道。 |
leave(name,uid,sid,cb) | GlobalChannelService.prototype.leave() name:信道名稱;uid:用戶id;sid:前端服務id;cb:回調函數。 從信道中移除成員。 |
pushMessage() | GlobalChannelService.prototype.pushMessage(serverType, route, msg,channelName, opts, cb) serverType:前端服務的類型, route:路由, msg:需要推送的消息,channelName:信道名稱, opts:參數, cb:回調函數 通過全局信道發送消息 |
API | 說明 |
get(frontendId,sid,cb) | LocalSeesionService.prototype.get() frontendId:會話鏈接的前端服務id,sid:會話Id,cb:回調函數。根據前端服務和會話id獲得本地會話 |
getByUid(name,uid,sid,cb) | LocalSeesionService.prototype.getByUid() frontendId:會話鏈接的前端服務id,uid:綁定到會話的用戶id,cb:回調函數。args: cb(err, localSessions)。根據前端服務和用戶id獲取本地會話。 |
kickBySid(name,uid,sid,cb) | LocalSeesionService.prototype.kickBySid() frontendId:會話鏈接的前端服務id,sid:會話Id,cb:回調函數。根據會話id踢掉該會話。 |
kickByUid() | LocalSeesionService.prototype.kickByUid() frontendId:會話鏈接的前端服務id,uid:用戶id,cb:回調函數。根據用戶id踢掉該會話。 |
API | 說明 |
bind(uid,cb) | LocalSeesion.prototype.bind() uid:用戶編號;cb:回調函數。callfunction(err)。綁定當前會話,用於前端服務的推送和全局會話的綁定。 |
unbind(uid,cb) | LocalSeesion.prototype.unbind() uid:用戶編號;cb:回調函數。callfunction(err)。取消綁定。 |
set(key,value) | LocalSeesion.prototype.set() 將key/value添加到本地會話中 |
get(key) | LocalSeesion.prototype.get() 根據key從本地會話中獲取值。 |
push(key,cb) | LocalSeesion.prototype.push() 將本地會話中的key/value添加到全局會話中 |
pushAll(cb) | LocalSeesion.prototype.pushAll() 將本地會話中的所有key/value添加到全會話中 |
API | 說明 |
kick(uid,cb) | SeesionService.prototype.kick() 踢掉該用戶的所有離線會話 |
kickBySession(sid,cb) | SeesionService.prototype.kickBySession() sid:會話編號;cb:回調函數。根據會話id踢掉一個在線用戶 |
sendMessage(sid,msg) | SeesionService.prototype.sendMessage()根據會話id向客戶端發送消息 |
sendMessageByUid(uid,msg) | SeesionService.prototype.sendMessageByUid() 根據用戶id向客戶端發送消息 |
API | 說明 |
createApp(opts) | Pomelo.create() 創建一個Pomelo 應用程序 |
對於Pomelo的API有了一個大致的了解,在《Pomelo API》中,提供了API原版的說明和部分源碼的實現。今天的學習就到這里,明天繼續。