Web application push API

Source: Internet
Author: User

(See: http://www.xdarui.com/index.php/archives/111)

In 18th day of this month W3C released a new draft "Push API", the original see here: http://www.w3.org/TR/push-api. It's just a bit of ignorance. Below, I would like to share this draft with you. Of course, I suggest you directly ignore this article and study it directly at the W3C original address. Because it is only a draft and may be updated, replaced, or discarded at any time, who knows what tomorrow will look like.

About push

The content of this article has been carefully written by two bloggers. I will post the original article address here. If you do not have any background knowledge, please follow the steps below: 1. Mobile push mechanismHttp://blog.163.com/fuhaocn@126/blog/static/36665080201204103631378/2. About mobile pushHttp://www.cnblogs.com/aspnethot/articles/2258724.html 

Webapps push

Webapps means that many Web websites are separated from simple display or form submission. For example, a rich front-end application such as Gmail has risen to an application.ProgramThis application is only on the web end, mainlyCodeIt consists of JavaScript. Now, let's talk about the subject directly. The draft push API aims to provide the Web Front-end with the ability to process server push messages. The push service has nothing to do with the active status of Web applications. Push messages can be distributed through a variety of standard protocols (such as SSE/GSM-SMS/SIP Message/OWA push), or through a specific browser method.

The workflow of push is similar to that of mobile phones. First, the program must generate a unique token, and then the web program can establish a push channel with the server after the user displays the authorization and consent.

For demonstration, I moved the W3C example directly:

 

Example 1

VaRToken=Getguid(); // Get guid as unique token

VaRMypush=Requestremotepermission(Token);

Mypush.Onerror= Function () {

Alert("Darn! "+E.Message);

};

Mypush.Onsuccess= Function () {

VaRActivate= "Http://example.com/push/activate? Token ="+Token+

"& Serviceurl ="+Encodeuricomponent(Mypush.Serviceurl) +

"& Serverprotocols ="+Mypush.Serverprotocols;

Asyncxhr(Activate); // Send activation request to Application Server

Mypush.Wakeup= True;

Mypush.Onmessage= Function (Event) {

Document.Getelementbyid('Message').Innerhtml= Event.Data;

};

};

 

As described in the draft, navigator needs to implement the navigatorpush interface, which defines the push attribute (readonly ). The draft also defines another interface: pushmanager. (As described in the draft, window will implement this interface ). Pushmanager provides two methods:

Checkremotepermission and requestremotepermission. The checkremotepermission method can be used to check whether the current web application has been authorized by the user (this process does not require user intervention ), if the current program has been authorized, the success callback will be called; otherwise, the error callback will be executed. The success callback can return a pullservice object (the same as the one obtained by calling the requestremotepermission method ). Requestremotepermission first queries the user's authorization through the user proxy (generally we refer to the browser, if the user agrees, the unique token generated by the program will become the address (target service) of the PUSH Service and activate the PUSH Service. The following are related IDL definitions:

[Nointerfaceobject]

InterfaceNavigatorpush{

Readonly attribute pushmanager push;

};

InterfacePushmanager{

Pushservice requestremotepermission (Domstring Apptoken, OptionalDomstring Publickey, OptionalDomstring Algorithm);

Pushservice Checkremotepermission(Domstring Apptoken);

};

 

Pushservice Interface

The web program processes the PUSH Service through the pushservice interface. It is defined as follows:

InterfacePushservice:Eventtarget{

Readonly attributeDomstringApptoken>;

Readonly attributeDomstringPublickey;

Readonly attributeDomstringAlgorithm;

Readonly attributeDomstringServiceurl;

Readonly attributeDomstring []Serverprotocols;

Readonly attributeDomerrorError;

AttributeBooleanWakeup;

AttributeFunction?Onsuccess;

AttributeFunction?Onerror;

AttributeFunction?Onmessage;

Void Close();

Readonly attributeUnsigned shortReadystate;

ConstUnsigned short Connecting= 0;

ConstUnsigned short Open= 1;

ConstUnsigned short Closed= 2;

};

Note:

 

1. if multiple web programs generate the same token, the push message sent to this token as the address will be sent to these instances at the same time. In other words, if a and B register with the ticket t at the same time, the registration department will notify both A and B when the notice t is available.

2. to verify that the push message source is correct, the application service can sign the PUSH message. If this is done, pushservice should have its corresponding publickey and algorithm (Algorithm).

3. The Push Service has nothing to do with whether the web program is active. The same web program can also register multiple push services. Multiple instances of the same web program can also register their own push services.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.