8. Mobile Service重點功能
• DATA
• User Authorization
• Push Notification
• Server Script
• Scale、Logging、Diagnastics
9. DATA
• 提供存取 Windows Azure 的儲存體
• SQL 資料庫
• Blob 儲存體
• 資料表儲存體
• 協力廠商資料服務,如 Mongo DB 等
• Mobile Service和 Service Bus Transfer
• 提供了連接到內部部署資料庫的選項
• 敏感性資料未儲存在雲端亦可供行動裝置存取
18. Get Start
• 登入Azure入口網址
• Windows Live ID
• 建立Mobile Service
• 建立一個Data Table
• 下載SDK或自動產生的專案
• 利用App操作Mobile Service更新資料
http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-data-dotnet/?fb=zh-tw
48. Push Notification
• Windows Store
• Windows Push Notification Services (WNS)
• Windows Phone
• Microsoft Push Notification Service (MPNS)
• Google Cloud Messaging (GCM)
• Android
• Apple Push Notifications (APNs)
• iOS
68. 重點提醒
• Sever Script
• 針對發送平台具有不同指令
• 註冊Channel需做重覆檢查
• Android / iOS獨立需具備的要素
• 憑證、Google Console API
• MPNs推送類型有Tile、Toast
69. User AUTH
how to authenticate users in Windows Azure Mobile Services
from your app
70. User Auth原理 – 1/3
• 整合各種驗證用戶機制
• Microsoft Account
• Facebook login
• Twitter login
• Google login
• 減少需獨立開發帳號管理
• 減少個資外洩
• 無需負擔帳號成本
71. User Auth原理 – 2/3
auth
Microsoft
Account
Request authorization
Client
Application
Return token
token
auth
auth
Transaction by the token
auth
Facebook
login
Twitter
login
Google
logic
72. User Auth原理 – 3/3
https://www.ibm.com/developerworks/community/blogs/48a78681-82cc-434f-9c78-3e9117bfd466/resource/BLOGS_UPLOADED_IMAGES/Oauth_Basic_Flow.JPG?lang=en
73. Get started with
authentication
• Facebook login
• Google logic
• Microsoft Account
• http://www.windowsazure.com/en-us/develop/mobile/tutorials/getstarted-with-users-wp8/?fb=zh-tw
• http://www.windowsazure.com/en-us/develop/mobile/how-toguides/register-for-microsoft-authentication/?fb=zh-tw
• Twitter login
• http://www.windowsazure.com/en-us/develop/mobile/how-toguides/register-for-twitter-authentication/?fb=zh-tw
91. Server Script – 1/3
• 支援自訂邏輯,儲存於Server
• 類似JavaScript語法
• 功能
• CRUD、Schedule Jobs、HTTP methods define in a
custom API
• 基於REST APIs為底
• Script不保存執行狀態
• 每一次請求均是新的開始,狀態均被初始化
• 如需保存,請另寫入tables
http://msdn.microsoft.com/en-us/library/windowsazure/jj554226.aspx (完整指令教學)
92. Server Script – 2/3
Script
apns object
gcm object
push object
request object
response object
service object
mpns object
mssql object
update function
delete function
insert function
read function
statusCodes object
table object
user object
wns object
query object
console object
93. Server Script – 3/3
• Table operations
• Custom API
• Job Scheduler
• Using Command line tool (教學)
• Working with tables
• Debugging and troubleshooting
97. Table operations – 2/4
• 固定三個參數
• first
• insert/update
• item object,屬JSON物件,代表一個Row,透過Name來
操作,例如:item.Owner
• delete
• the ID of the record to delete
• read
• query object,代表一個row set
• second
• user object
• third
• request object
• 控制request的執行與response至client的內容
98. Table operations – 3/4
• 重點
• Insert: function insert (item, user, request) { … }
• Update: function update (item, user, request) { … }
• Delete: function del (id, user, request) { … }
• Read: function read (query, user, request) { … }
99. Table operations – 4/4
• Register for table operations
• Override the default response
• Override the execute success
• Override default error handling
• Add custom parameters
• Work with table users
100. Register for table
operations – 1/2
• Azure Management Portal
• 定義Mobile Service每個tables的opertaions
• Source Control
• 建立<table>.<opertaion>.js
• 放置.servertable的子目錄
• 執行時,自動取得該js進行
• Common line tool
101. Register for table
operations – 2/2
• Table operation script重點二個function
• execute function
• request已被operation完成
• 回傳一個標準的response
• respond function
• 自訂response
For more examples, see Read and write data, Modify the request and Validate data.
107. Work with users
• Mobile Service提供身份驗證功能
• 當驗證用戶執行table operations時
• user object
• 出現於operations的參數之中
• user object.userid
• userid屬性可用於儲存與擷取用戶個別設定資訊
109. Custom API – 1/2
• 在Mobile Service建立自訂endpoint
• 藉由standard HTTP methods
• GET、POST、PUT、PATCH、DELETE
• 每一個API可自訂於單一個script file
• Mobile service runtime收到Client請求
• 轉發給Custom API
• API中搭配express.js library加以操作
https://<service_name>.azure-mobile.net/api/<api_name>
110. Custom API – 2/2
• Define a custom API
• Implement HTTP methods
• Send and receive data as XML
• Work with user and headers in a custom API
• Define multiple routers in a custom API
112. Define a custom API – 2/2
• 每一個HTTP Method均會有request / response
• {}操作的回應項目,來自express.js library
• 回傳格式為JSON
113. Implement HTTP methods
• Standard HTTP Method
exports.get = function(request, response) { ... };
exports.post = function(request, response) { ... };
exports.patch = function(request, response) { ... };
exports.put = function(request, response) { ... };
exports.delete = function(request, response) { ... };
• The custom API endpoint cannot be called using
an HTTP method that has not been
implemented in the server script
• a 405 (Method Not Allowed) error response is
returned
• Separate permission levels can be assigned
to each support HTTP method.
114. Send and receive data as
XML
• Mobile Service使用JSON呈現資料給Client
• 想用XML的結構傳遞
• 相關範例
• Define a custom API that supports periodic
notifications
115. Work with users and
headers in a custom API –
1/3
• User object
• Custom API藉由request object取得
• request.user.userId
• HTTP Headers
• 夾帶描述request、response的說明
• Host、user-agent、content-type…等
• 定義交易的類型
• Key/value資料型態
116. Work with users and
headers in a custom API –
2/3
• 取得user的table
• 藉由指定user.userid來搜尋對應的資料
117. Work with users and
headers in a custom API –
3/3
• 取得request中header
• my-custom-header
• 相關request / response object操作
• http://expressjs.com/api.html#res.status
118. Define multiple routes in a
custom API – 1/2
• Mobile Service支援Custom API
• 定義multiple path / routes
• 例如
• 定義一個HTTP GET方法:calculator
• 可再定義二個sub function
• add
• subtract
https://<service>.azure-mobile.net/api/calculator/add
https://<service>.azure-mobile.net/api/calculator/sub
119. Define multiple routes in a
custom API – 2/2
• 利用 exports.register 定義multiple path
• 藉由api object註冊get,post,put,patch,delete
依照HTTP Method可加
以區分設定
固定二個參數
First:route名稱
Second:function
120. Call a custom API from the
client
• 範例練習
• http://www.windowsazure.com/enus/develop/mobile/tutorials/call-custom-apiwp8/?fb=zh-tw
125. Schedule recurring jobs in
Mobile Services
• 範例練習
• Schedule recurring jobs in Mobile Services
• http://www.windowsazure.com/enus/develop/mobile/tutorials/schedule-backendtasks/?fb=zh-tw
• 結合Twitter進行取得更新資料
127. Source control, share
code, … - 1/3
• Mobile Service使用Node.js
• 自訂的script,直接被加入Node.js的module
• 藉由source control
• 自訂module
• 新增其他Node.js至Service中
128. Source control, share
code, … - 2/3
• global require function
http://www.windowsazure.com/en-us/develop/mobile/how-to-guides/work-with-server-scripts/?fb=zh-tw#scheduler-scripts
129. Source control, share
code, … -3/3
• How to: load Node.js modules
• Share code by using source control
• Leverage shared code and Node.js modules in
your server scripts.
• Use helper functions
• Work with app settings
130. Use helper functions – 1/2
• helper function
• 需宣告在main function之後
• 需要的參數均要先全部定義好
• 寫在同一個script裡,屬於區域方法
136. Working with tables – 1/2
• 大部分情境中的Server Script會操作tables
• 例如
• Mobile Service不具有狀態,如需保存則需tables
• 搭配權限控制request存取的能力
• 二大方式連接tables
• table object
• 簡易式操作資料表資訊
• mssql object
• 類似用T-SQL指令方式
• 提供更彈性與複雜的指令
137. Working with tables – 2/2
• Access tables from scripts
• Perform Bulk Insert
• Map JSON types to database types
• Using Transact-SQL to access tables
138. Access tables from scripts –
1/3
• table object
• getTable function取得指定資料表物件
• 指令於不同的模組用法不同
• table operations / schedule jobs
• custom api
• 在shared function不可使用table object
• 要用參數傳入
144. Map JSON types to database
types – 2/2
• JavaScirpt objects與T-SQL type類型對應表
JavaScript property
Number
Boolean
Date
String
Buffer
Object
Array
Stream
T-SQL type
Float(53)
Bit
DateTimeOffset(3)
Nvarchar(max)
Not supported
Not supported
Not supported
Not supported
146. Using Transact-SQL to
access tables – 2/2
• Run a static query
• Run a dynamic query
• Join relational tables
• Run a query that return raw results
• Get access to a database connection
147. Run a static query
• 執行一個固定沒有傳入參數的查詢
• 回傳為JSON格式
148. Run a dynamic parameterized
query
• 執行時夾帶參數,搭配User Auth進行
160. References – 1/2
• Work with server scripts in Mobile Services
• Schedule recurring jobs in Mobile Services
• Send email from Mobile Services with
SendGrid
• Upload images to Windows Azure Storage by
using Mobile Services
161. References – 2/2
• 透過 Windows Azure Mobile Services 開發各平台 Apps
• Windows Azure Mobile Services - The Perfect Partner
• SolidQ Windows Azure Mobile Services & Windows 8
• Introducing Azure Mobile Services – Script and Custom API
• Introducing Windows Azure Mobile Services – Authentication
• Introducing Windows Azure Mobile Services
• http://www.windowsazure.com/en-us/develop/mobile/?fb=zh-tw
• Client library for .NET
• Client library for Javascript
• REST API reference