I will try to explain myself the best than I can!
I have a node.js server and I want to add socket.io. I manage to get it running on http but I'm having some problems running it on https.
Server:
var express = require('express');
var app = express();
var fs = require('fs');
var key = fs.readFileSync('mydomain.key');
var cert = fs.readFileSync( 'mydomain.crt' );
var options = {
key: key,
cert: cert
};
var https = require('https');
var io = require("socket.io")(https);
var cors = require('cors')
var server = https.createServer(options, app).listen(443);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", 'domain');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Credentials', true);
next();
server.on('error', function (e) {
console.log(e);
});
io.on('connection',function(socket){
console.log("A user is connected");
socket.on('add reserva',function(status){
//io.sockets.emit('update reserva', status);
add_reserva(status,function(res){
if(res){io.sockets.emit('update reserva', status);}
else {io.sockets.emit('error');}
});
});
});
**And the client code:**
$(document).ready(function(){
var socket = io("https://151.236.63.10:443");
$("#add_status").click(function(){
socket.emit('add reserva',$("#comment").val());
});
socket.on('update reserva',function(msg){
new Noty({
type: 'success',
layout: 'topRight',
text: msg
}).show();
});
});
});
In addicion, if there is something I could do better with the code, I would appreciate any advice