javascript - does service worker request, response from server continuously? -
i'm using server send event display notification.i have created service worker , used eventsource connect server (in case used servlet. ) once run project. working fine.
but contents inside event execute countiously. want know why? other question is
once close tab. stops sending notification. service worker nunning , server running. why stops?
this service worker code.
var eventsource = new eventsource("helloserv"); //mydiv1 custom event eventsource.addeventlistener("mydiv1",function(event){ console.log("data down" , event.data); var title = event.data; //below notification displaying continuously. why ? var notification = new notification(title, { icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png', body: event.data, }); notification.onclick = function () { window.open("http://ageofthecustomer.com/wp-content/uploads/2014/03/success.jpg"); }; console.log("down"); });
this servlet code;
response.setcontenttype("text/event-stream"); response.setcharacterencoding("utf-8"); printwriter writer = response.getwriter(); string upvote = "my vote"; writer.write("id:1\n"); writer.write("event:myid\n"); writer.write("data: "+ upvote +"\n"); writer.write("data: "+"new data 2\n\n"); system.out.println("servlet "+ i); writer.flush(); i++; writer.close();
service workers have limited lifetime, shouldn't use things web sockets or server sent events. push notifications implemented in different way.
in page, need subscribe user push notifications. subscription endpoint url (and set of keys, if plan use payloads). once user subscribed, need send subscription information server.
the server send push notification user via post request endpoint url.
the service worker awakened when push notification arrives, 'push' event handler going executed.
a simple example (for more complex ones, take @ serviceworker cookbook).
page
// register service worker. navigator.serviceworker.register('service-worker.js') .then(function(registration) { // use pushmanager user's subscription push service. return registration.pushmanager.getsubscription() .then(function(subscription) { // if subscription found, return it. if (subscription) { return subscription; } // otherwise, subscribe user (uservisibleonly allows // specify don't plan send notifications // don't have visible effect user). return registration.pushmanager.subscribe({ uservisibleonly: true }); }); }).then(function(subscription) { // subscription.endpoint endpoint url want // send server (e.g. via fetch api or via // xmlhttprequest). console.log(subscription.endpoint); // here's example fetch api: fetch('./register', { method: 'post', headers: { 'content-type': 'application/json' }, body: json.stringify({ endpoint: subscription.endpoint, }), }); });
service worker
// register event listener 'push' event. self.addeventlistener('push', function(event) { // keep service worker alive until notification created. event.waituntil( self.registration.shownotification('title', { body: 'body', }) ); });
server
in server, send post request endpoint url. example, curl:
curl -x post [endpointurl]
or, if you're using node.js, can use web-push library (https://github.com/marco-c/web-push):
var webpush = require('web-push'); webpush.sendnotification(req.query.endpoint, req.query.ttl);
in java, use class (https://github.com/marco-c/java-web-push) hides details of implementation , differences between protocols in current versions of firefox , chrome (differences destined disappear since chrome going use web push protocol soon). here's "manual" example push service implements web push protocol (currently works firefox):
url url = new url(endpointurl); httpurlconnection conn = (httpurlconnection)url.openconnection(); conn.setdooutput(true); conn.setrequestmethod("post"); outputstreamwriter writer = new outputstreamwriter(conn.getoutputstream()); writer.write(""); writer.flush(); string line; bufferedreader reader = new bufferedreader(new inputstreamreader(conn.getinputstream())); while ((line = reader.readline()) != null) { system.out.println(line); } writer.close(); reader.close();
Comments
Post a Comment