NodeJS HTTPS仅适用于chrome,而不适用于其他浏览器

时间:2014-03-25 01:28:48

标签: javascript node.js google-chrome https cross-browser

我尝试使用nodeJS处理一个简单的聊天应用。我希望它也适用于https。 提供了ssl证书和密钥,我有这个代码:

HTML

<div class="chat_box">
    <div>
        <table id="chat_panel"></table>
        <input type="hidden" name="chat_time" id="chat_time" value="12:00 AM" />
    </div>
    <table>
        <tr>
            <td colspan="2">Message :
                <textarea rows="3" name="chat_message" id="chat_message"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="chat_name" id="chat_name" class="fullspan" />
            </td>
            <td>
                <input type="button" name="send_message" id="send_message" value="SEND" />
            </td>
        </tr>
    </table>
</div>

SERVER.JS

// global vars
var http_port   = 8080;
var https_port  = 8079;
var host        = '127.0.0.1'; // IP of the host

// import modules
var fs      = require('fs');
var http    = require('http');
var https   = require('https');
var express = require('express');
var request = require('request');
var socketio= require('socket.io');

// certificate
var privateKey  = fs.readFileSync('sslcert/server.key', 'utf8');
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
var credentials = {
    key: privateKey,
    cert: certificate
};

var app         = express();
var httpServer  = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
var io          = socketio.listen(httpsServer);

// receive/request
io.sockets.on('connection', function(client) {
    console.log("New Connection Detected.");

    client.on('message', function(data) {
        console.log('Recieved:'+data);

        var sender  = data.name;
        var value   = data.chat;
        var time    = data.time;

        var dom = '<tr><td class="sender">' + sender + '</td><td class="message">' + value + '</td><td class="sender">' + time + '</td></tr>';

        // process recieved data
        io.sockets.emit('message', dom);
    });

});

// activate server
httpServer.listen(http_port, host);
httpsServer.listen(https_port, host);

// Success Message
console.log('Connection Established on '+http_port+'/'+https_port+' : '+host+'. Press CTRL + C to Disconnect.');

MAIN.JS

var http_port   = 8080;
var https_port  = 8079;
var host        = '127.0.0.1';
var socket      = io.connect(host+':'+https_port);

$('#send_message').bind('click', function(e) {
    e.preventDefault();
    chat();
});

function chat() {
    var data = {
        name: $('#chat_name').val(),
        chat: $('#chat_message').val(),
        time: $('#chat_time').val()
    };

    socket.emit('message', data);
}
socket.on('message', function(data) {
    $('#chat_panel').append(data); 
});

使用https时,我在谷歌浏览器中没有问题,但是当我尝试使用其他浏览器时,它无效。

Mozilla

我还尝试启用http,它适用于所有浏览器。这是一个代码问题还是我需要一个有效/真实的ssl证书,因为我只使用XAMPP的免费/测试副本。

0 个答案:

没有答案