|

- UID
- 256
- 帖子
- 137
- 精华
- 0
- 积分
- 1
|
3#
发表于 2009-6-28 09:06
| 只看该作者
Device-MojoMsgSample部分代码- /* This is a sample app that shows how to use the mojo messaging service.
- The app works in tandem with a cloud push app that runs at TODO.
- The first time this app is launched it creates an endpoint for itself
- and lets the clould app know about it. The cloud app also creates a
- device id to identify your device. It also has a web UI interface
- that lets you put your device id and push messages to your device.
- You can use some kind of login mechanism or the device nduid (see SDK)
- to uniquely identify a device from your cloud app. */
- function MojomsgAssistant() {
- }
- MojomsgAssistant.prototype.setup = function(){
- // Cookie is used as a local preference to store the fact that
- // we have created a node and sent it to the cloud app.
- this.cookie = new Mojo.Model.Cookie("cloudPush");
- this.deviceId = this.cookie.get();
-
- if (this.deviceId === undefined) {
-
- // The action below should be performed only once successfully
- // during the lifetime of the app.
- this.initSpinner();
- this.setupList();
- // Calling getConnectStatus() should create an endpoint and get a publish key
- // and share the key with the cloud app. You can go to the cloud app after this
- // to push messages to this device by entering the device Id.
-
- // Get Connection Status->CreateEndpoint->SubscribeEndpoint->Send key to Cloud,write to cookie
- this.getConnectionStatus();
- }
- else {
- this.setupLabels();
- this.setupList();
- this.subscribeToEndpoint();
- }
- }
- MojomsgAssistant.prototype.initSpinner = function(){
- this.controller.get('label').style.visibility = "visible";
- this.controller.get('pushList').style.visibility = "hidden";
- this.controller.setupWidget("spin",
- this.spinattributes = {
- spinnerSize: 'large'
- },
- this.spinmodel = {
- spinning: true
- });
- }
- MojomsgAssistant.prototype.stopSpinner = function() {
- this.spinmodel.spinning = false;
- this.controller.modelChanged(this.spinmodel, this);
- this.controller.get('spin').style.visibility = "hidden";
- this.controller.get('pushList').style.visibility = "visible";
- }
- MojomsgAssistant.prototype.getConnectionStatus = function(){
- this.controller.get('statusLabel').innerHTML = 'Getting mojo msg service connect status...';
- this.controller.serviceRequest('luna://com.palm.pubsubservice', {
- method: 'connectStatus',
- parameters: {"subscribe" : true},
- onSuccess: this.connStatusSuccess.bind(this),
- onFailure: this.connStatusFailure.bind(this)
- });
- }
- MojomsgAssistant.prototype.connStatusSuccess = function(response){
- if (response.connected) {
- this.controller.get('statusLabel').innerHTML = 'Mojo msg service connected';
- this.connected = true;
- this.createEndpoint(); // We are connected, so create the endpoint
-
- }
- else {
- this.connected = false;
- this.controller.get('statusLabel').style.color = "red";
- this.controller.get('statusLabel').innerHTML = 'Mojo msg service not connected';
- }
- }
- MojomsgAssistant.prototype.connStatusFailure = function(){
- this.connected = false;
- this.controller.get('statusLabel').style.color = "red";
- this.controller.get('statusLabel').innerHTML = 'Mojo msg service not connected';
- }
- MojomsgAssistant.prototype.createEndpoint = function(){
- this.controller.get('statusLabel').innerHTML = 'Creating Endpoint...';
- this.controller.serviceRequest('luna://com.palm.pubsubservice', {
- method: 'createEndpoint',
- parameters: {"endpoint" : "cloudPush", "description": "Endpoint that recvs msgs from cloud"},
- onSuccess: this.createEndpointSuccess.bind(this),
- onFailure: this.createEndpointFailure.bind(this)
- });
- }
- MojomsgAssistant.prototype.createEndpointSuccess = function(response){
- this.controller.get('statusLabel').innerHTML = 'Endpoint ' + response.endpoint + ' created';
- this.publishKey = response.publishKey;
- this.subscribeToEndpoint(); // Subscribe to the Endpoint to recv messages
- }
- MojomsgAssistant.prototype.createEndpointFailure = function(response){
- this.controller.get('statusLabel').style.color = "red";
- this.controller.get('statusLabel').innerHTML = 'createEndpoint failed : ' + response.errorText;
- }
- MojomsgAssistant.prototype.subscribeToEndpoint = function(){
- this.controller.get('statusLabel').innerHTML = 'Subscribing to Endpoint...';
- this.controller.serviceRequest('luna://com.palm.pubsubservice', {
- method: 'subscribe',
- parameters: {"endpoint" : "cloudPush", "subscribe" : true},
- onSuccess: this.subscribeSuccess.bind(this),
- onFailure: this.subscribeFailure.bind(this)
- });
- }
- MojomsgAssistant.prototype.subscribeSuccess = function(response){
- this.controller.get('statusLabel').innerHTML = 'Endpoint ' + response.endpoint + ' subscribed';
- // A subscribe will have more than one response. Once to say subscribe succeeded,
- // and one more time with the item if a message got pushed.
- if (response.item === undefined) {
- if (this.deviceId === undefined) { // If this is the first time
- this.shareKey();
- }
- }
- else { // We have an item
- this.addItemToList(response.item);
- this.subscribeToEndpoint(); // Reprime subscribe
- }
- }
- MojomsgAssistant.prototype.subscribeFailure = function(response){
- this.controller.get('statusLabel').style.color = "red";
- this.controller.get('statusLabel').innerHTML = 'subscribe failed : ' + response.errorText;
- }
- MojomsgAssistant.prototype.shareKey = function(){
- this.controller.get('statusLabel').innerHTML = 'Sharing publish key with Cloud App';
- var URL = "http://your-ip-address:8080/pushexampleapp/register?p=" + this.publishKey;
- var req = new Ajax.Request(URL, {
- method:'get',
- onSuccess: this.shareKeySuccess.bind(this),
- onFailure: this.shareKeyFailure.bind(this)
- });
- }
- MojomsgAssistant.prototype.shareKeySuccess = function(transport) {
- var response = (transport.responseText).evalJSON();
- if (response.returnValue) {
- this.stopSpinner();
- this.saveDeviceId(response.deviceCode);
- this.setupLabels();
- }
- else {
- this.controller.get('statusLabel').style.color = "red";
- this.controller.get('statusLabel').innerHTML = 'Sharing Key failed : ' + response.errorText;
- }
- }
- MojomsgAssistant.prototype.shareKeyFailure = function(transport){
- this.controller.get('statusLabel').style.color = "red";
- this.controller.get('statusLabel').innerHTML = 'Sharing Key failed : ' + transport.status;
- }
- MojomsgAssistant.prototype.saveDeviceId = function(deviceCode) {
- this.cookie.put(deviceCode);
- }
- MojomsgAssistant.prototype.setupLabels = function() {
- this.controller.get('statusLabel').style.visibility = 'visible';
- this.controller.get('statusLabel').style.color = 'green';
- this.controller.get('label').style.visibility = "visible";
- this.deviceId = this.cookie.get();
- this.controller.get('label').innerHTML = "Device Id : " + this.deviceId;
- this.controller.get('statusLabel').innerHTML = 'To push messages - http://your-ip-address:8080/pushexampleapp/';
- }
- MojomsgAssistant.prototype.setupList = function(){
- this.listModel = {
- listTitle: "Recent Messages Pushed",
- items: [{
- data: "Your pushed messages will appear here",
- time: "Time of Message here"
- }]
- };
-
- this.controller.setupWidget("pushList",
- this.listAttr = {
- itemTemplate: "mojomsg/listItem",
- listTemplate: "mojomsg/listContainer",
- swipeToDelete: true,
- renderLimit: 40,
- reorderable: false,
- autoconfirmDelete: true
- },
- this.listModel
- );
- this.controller.modelChanged(this.listModel, this);
- }
- MojomsgAssistant.prototype.addItemToList = function(item){
- var obj = {data:item, time:new Date()};
- this.listModel.items.unshift(obj);
- this.controller.modelChanged(this.listModel, this);
- }
复制代码 |
|