转移到HTTPS后,推送通知无效

时间:2018-03-13 09:08:20

标签: javascript node.js express push-notification socket.io

我正在研究如何在新安全的tomcat服务器上运行推送通知。我的通知在nodejs和socket.io模块上运行。在转移到HTTPS之前它工作正常。现在它在控制台上抛出错误说:

Mixed Content: The page at 'https://example.com/home.jsp#' was loaded over HTTPS, but requested an insecure script 'http://example.com:8002/socket.io/socket.io.js'. This request has been blocked; the content must be served over HTTPS.

Uncaught ReferenceError: io is not defined
at home.jsp:812

这是我的节点js脚本w / c声明变量的部分:

var sys = require('sys');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
sys.puts(stdout);
sys.puts(error);
}
var nodemailer = require("nodemailer");
var smtpTransport = require("nodemailer-smtp-transport");
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
mysql = require('mysql'),
connectionsArray = [],
connection = mysql.createConnection({
host: '127.0.0.1',
user: 'sampleUser',
password: 'samplePassword',
database: 'sampleDB',
port: 3306
}),
POLLING_INTERVAL = 2000,
pollingTimer;  
var smtpTransport = nodemailer.createTransport(smtpTransport({
host : "smtp.gmail.com",
secureConnection : false,
port: 587,
auth : {
    user : "sample@email.com",
    pass : "sampleEmailPass"
}
}));

这是我的jsp文件:

<html>
<head>
<!-- 
 * Author:      Gianluca Guarini
 * Contact:     gianluca.guarini@gmail.com
 * Website:     http://www.gianlucaguarini.com/
 * Twitter:     @gianlucaguarini
-->
    <title>Push notification server streaming on a MySQL db</title>
    <style>
    </style>
</head>
<body>
    <time></time>
    <div id="container"></div>
<script src="http://example.com:8002/socket.io/socket.io.js"></script>
<script>

    // create a new websocket
    var socket = io.connect('http://example.com:8002');
    // on message received we print all the data inside the #container div
    socket.on('notification', function (data) {
    var usersList = "";

    $.each(data.users,function(index,user){
        if (user.num_count!=0) {
            if(<%=session.getAttribute("accessID")%>==user.user_login_id)
        {

        usersList += user.num_count;
        }
    }
    });

    if ($('#container').text() < usersList)
    {
      var diff = usersList-$('#container').text();
      notifyMe(usersList);
      /*
      for(var e =0; e<diff; e++)
      {

      notifyMe(diff);
      }*/
    }

   $('#container').html(usersList);

   // $('time').html('Last Update:' + data.time);
  });
  function notifyMe(usersList) {
  // Let's check if the browser supports notifications
   var options = {
    icon: '/icon/main_page/main_icon.png',
    sound: '/icon/sounds/Water_DROP.mp3'
  }
  if (!("Notification" in window)) {
  alert("This browser does not support desktop notification");
return false;
  }

  // Let's check whether notification permissions have already been granted
  else if (Notification.permission === "granted") {
  // If it's okay let's create a notification
   var notification = new Notification('You have '+usersList+' item(s) for approval',options);

  notification.sound;
   }

  // Otherwise, we need to ask the user for permission
  else if (Notification.permission !== 'denied') {
  Notification.requestPermission(function (permission) {
  // If the user accepts, let's create a notification
  if (permission === "granted") {
    var notification = new Notification('You have '+usersList+' item(s) for approval',options);

  notification.sound;
  }
  });
  }
  notification.onclick = function(){
  window.focus();
  };
   // At last, if the user has denied notifications, and you 
    // want to be respectful there is no need to bother them any more.
    }   
</script>
</body>

到目前为止我所做的是将所有http翻译为https。它解决了混合类型错误,但没有解决未定义的io情况。我搜索了一些主题,但它使用了express.js,它需要一个密钥和证书。

TIA

1 个答案:

答案 0 :(得分:-1)

非常简单的解决方案。 只需更改你的&#34; socket.io.js&#34;脚本要通过https服务而不是http协议。 例如: &#39; https://yoursite.com/socket.io.js&#39;而不是&#39; http://yoursite.com/socket.io.js&#39;