Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Socket.io-通过https从客户端连接到服务器_Javascript_Node.js_Socket.io - Fatal编程技术网

Javascript Socket.io-通过https从客户端连接到服务器

Javascript Socket.io-通过https从客户端连接到服务器,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我已经在我的虚拟服务器(Ubuntu)上创建了一个socket.io聊天应用程序,它作为systemd服务运行,并且处于活动运行状态 My server.js位于: /var/www/vhosts/mywebpage.de/w1.mywebpage.de/chat/ server.js如下所示: const io = require('socket.io')(3055); io.on('connection', function(socket) { // When the clie

我已经在我的虚拟服务器(Ubuntu)上创建了一个socket.io聊天应用程序,它作为systemd服务运行,并且处于活动运行状态

My server.js位于:

/var/www/vhosts/mywebpage.de/w1.mywebpage.de/chat/
server.js如下所示:

const io = require('socket.io')(3055);

io.on('connection', function(socket) {

    // When the client emits 'addUser', this listens and executes
    socket.on('addUser', function(username, room) {
        ...
    });

    // When the client emits 'sendMessage', this listens and executes
    socket.on('sendMessage', function(msg) {
        ...
    });

    // Disconnect the user
    socket.on('disconnectUser', function(username, room) {
        ...
    });

});
在我的网站(https)中,我尝试按以下方式连接:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>

<script type="text/javascript">
    var loSocket;
    $(document).ready(function() {
        if(typeof(loSocket) == 'undefined') {
            loSocket = io('https://w1.mywebpage.de:3055', {
                reconnectionAttempts: 5,
                forceNew: true
            });
        }
    });
</script>

可能是什么错误?

根据我过去所做的工作,我将创建一个服务于SSL证书的服务器,并使用您创建的https服务器创建套接字服务器,这将允许您通过https进行连接,并且您需要在socketio()上启用
安全

使用此代码作为如何使用http创建socketio服务器的参考 您可以在


注意:您需要使用非http(如示例所示)

根据我过去所做的,我将创建一个服务SSL证书的服务器,并使用您创建的https服务器创建套接字服务器,这将允许您通过https进行连接,并且您需要在socketio()上启用
安全

使用此代码作为如何使用http创建socketio服务器的参考 您可以在


注意:您需要避免使用示例中所示的http,socket.io在其上没有Web服务器,因此您需要在server.js中使用节点http或expressjs来设置服务器。看,但大约两年前,我得到了同样的server.js工作。我不知道从那以后发生了什么变化。server.js与/usr/bin/node.com一起永久运行。好吧,socket.io在其上没有Web服务器,因此您需要在server.js中使用node http或expressjs来设置服务器。看,但大约两年前,我得到了同样的server.js工作。我不知道从那以后发生了什么变化。Serv.js是用/Ur/bin / NoDE永久运行的。谢谢,但是我不认为这是个问题,因为你可以从中间看到…var socket=io('socket.io')(端口)。。。已经创建了一个http服务器。如果您不提供http服务器,它会创建一个,如果您创建自己的并使用它创建套接字服务器,它将使用您创建的一个。@Kelturio它创建一个http服务器,而不是HTTPS服务器。您可以通过
var app=require('HTTPS')。createServer({key:'',cert:'});var io=require('socket.io')(应用程序)我也可以做这个没有“钥匙”和“证书”?谢谢,但我不认为这是个问题,因为你可以从中间看到…var socket=io('socket.io')(端口)。。。已经创建了一个http服务器。如果您不提供http服务器,它会创建一个,如果您创建自己的并使用它创建套接字服务器,它将使用您创建的一个。@Kelturio它创建一个http服务器,而不是HTTPS服务器。您可以通过
var app=require('HTTPS')。createServer({key:'',cert:'});var io=require('socket.io')(应用程序)我可以在没有“密钥”和“证书”的情况下执行此操作吗?
(failed) ERR_CONNECTION_CLOSED with initiator polling-xhr.js:264.
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
    function (err, data) {
        if (err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }

        res.writeHead(200);
        res.end(data);
    });
}

io.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});