Guest User

Untitled

a guest
Sep 21st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. const express = require('express'); //
  2. incluimos librerias
  3. const webpush = require('web-push');
  4. const bodyParser = require('body-parser');
  5. const path = require('path' );
  6. const mysql= require('mysql');
  7.  
  8. const app= express(); // creamos una variable express para la comunicacion
  9.  
  10. app.use(bodyParser.json());
  11.  
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13.  
  14.  
  15.  
  16.  
  17. function get_subsc_from_db(){
  18. conn=get_dataDB();
  19. var subscriptions=get_subs(conn);
  20. console.log("el codigo llega hasta aqui");
  21. return subscriptions;
  22.  
  23. }
  24.  
  25.  
  26.  
  27. function get_dataDB(){
  28. var connection=mysql.createConnection({
  29. host:'localhost',
  30. user:'usuario',
  31. password:'password',
  32. database:'nombre_de_basededatos',
  33. port:puerto
  34.  
  35. })
  36.  
  37. connection.connect(function(error){
  38.  
  39. if(error){
  40. throw error;
  41. }else{
  42. console.log('Conexion Correcta');
  43. }
  44. });
  45. return connection;
  46. }
  47. function get_subs(conn){
  48.  
  49. var subs=[];
  50. var aux=[];
  51.  
  52. conn.query("select Endpoint,Key_u,Auth from usuarios where IP = '::1';",function(error,resultado){
  53. if(error){throw error;}
  54.  
  55. else{
  56. for(i=0;i<resultado.length;i++){
  57. aux[i]={"endpoint" : resultado[i].Endpoint,
  58. "keys" :{
  59. "auth":resultado[i].Auth,
  60. "p256dh":resultado[i].Key_u
  61. }
  62. }
  63.  
  64. subs[i]=JSON.stringify(aux[i]); console.log(subs[i]);
  65.  
  66. }
  67.  
  68.  
  69.  
  70. }
  71. });
  72. conn.close();
  73. return subs;
  74. }
  75.  
  76.  
  77. ////////////////////////////////////////////////////////////////////////////
  78.  
  79.  
  80.  
  81.  
  82.  
  83. const publicVapidKey ='<llave publica>'; // tenemos las keys del servidor(privada y publica)
  84. const privateVapidKey ='<llave privada>';
  85.  
  86. webpush.setVapidDetails('mailto:mail@test.com',publicVapidKey,privateVapidKey);
  87.  
  88.  
  89.  
  90.  
  91. app.post('/api/trigger-push-msg/',function(req,res){
  92.  
  93. return get_subsc_from_db().then(function(subscriptions){
  94. let promiseChain = Promise.resolve();
  95. for(i=0;i<subscriptions.length;i++){
  96. const subscription = subscriptions[i];
  97. promiseChain = promiseChain.then(()=>{
  98. return triggerPushMsg(subscription,data);
  99. });
  100. }
  101. return promiseChain;
  102. })
  103.  
  104. });
  105.  
  106. const triggerPushMsg = function(subscription,dataToSend){
  107. return webpush.sendNotification(subscription,dataToSend)
  108. .catch((err) => {
  109. console.log('subscription is no longer valid:',err);
  110. });
  111.  
  112. };
Add Comment
Please, Sign In to add comment