套接字不能在HTTPS上工作但在HTTP上工作Socket.io

时间:2018-05-21 07:18:43

标签: node.js laravel redis socket.io

我已在我的服务器上配置了套接字设置,它正确使用HTTP但不使用HTTPS。

我已经尝试了所有的解决方案,但没有运气。 我已经通过以下命令生成了密钥和证书文件:

openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt

然后:

 var options = {
      key: fs.readFileSync('./key.pem', 'utf8'),
      cert: fs.readFileSync('./server.crt', 'utf8')
   };

但它给了我错误: enter image description here Server.js

var express = require('express'); //Node
var httpsapp = express();//Node 
var httpapp = express(); //Node 
fs = require('fs'); //
var ip = require("ip");
var underscore = require("underscore");
var _ = require('lodash');


options = {
    key: fs.readFileSync('./key.pem'),
    cert: fs.readFileSync('./server.crt')
}

http = require('http').createServer(httpapp)
httpsserver = require('https').createServer(options, httpsapp);
io = require('socket.io').listen(http);

var Redis = require('ioredis');

var redis = new Redis();
redis.subscribe('chatMessage','deleteMessageHard');

redis.on('message', function (channel, message) {
        console.log( ip.address() );


        if (channel == 'chatMessage') {
            io.emit('messageFound', {
                message: message
            });
        }
        console.log(channel);
        if (channel == 'deleteMessageHard') {
            io.emit('deleteHardChatFound', {
                message: message
            });
        }
    });

var connectedUsers = [];
var connectedUsersObj = {};
io.on('connection', function (socket) {

    //Login
    socket.on('login', function (data) {
        var user_id = parseInt(data.id);
        var user =  {   
                        "user_id": data.id,
                        "socket_id"  : socket.id,
                    }
        connectedUsers.push(user);
    });


    //Logout
    socket.on('disconnect', function(){
        var User = _.find(connectedUsers, {socket_id: socket.id});
    });

    socket.on('Typing', function (message) {
        var TypingFire = true;
        console.log('Typing Event Found');
        console.log(message);
        //io.emit('TypingFire', message);
        socket.broadcast.emit('TypingFire', message);
    });

    socket.on('stopTyping', function (message) {
        var TypingOff = true;
        console.log('stopTyping Event Found');
        //socket.emit('TypingOff', message);
        socket.broadcast.emit('TypingOff', message);
    });
});

httpsserver.listen(3002,'0.0.0.0'); //HTTPS
http.listen(3005,'0.0.0.0'); //HTTP

socket.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Socket</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <h1>HI</h1>
    <input type="text" id="Typing">
    <script>
        var socket = io('https://18.222.8.81:3002');

        $("#Typing").keyup(function(){
            socket.emit('Typing', { hello: 'world' });
        });

        socket.on('messageFound', function (data) {
            console.log(data);
        });
    </script>
</body>
</html>

enter image description here

我不知道这里缺少什么。任何帮助将不胜感激。

enter image description here

0 个答案:

没有答案