简体   繁体   English

node.js 套接字 io https

[英]node.js socket io https

I'm trying to establish a WebSocket (with node.js/socket.io) on an HTTPS protocol.我正在尝试在 HTTPS 协议上建立一个 WebSocket(使用 node.js/socket.io)。 But the client still keeps on polling and could not find the server although the server seems fine and listens.但是客户端仍然继续轮询并且无法找到服务器,尽管服务器看起来很好并且正在侦听。

This is what I have done so far.这是我迄今为止所做的。 Could you see anything wrong with it?你能看出它有什么问题吗?

My assumption is there is something not right with the certificates which I am using.我的假设是我使用的证书有问题。 I encrypted the server with plesk "lets encrypt" and took the certificates from this procedure is that right?我用plesk“让加密”加密了服务器并从这个过程中获取了证书,对吗?

---- server side ---- - - 服务器端 - -

var fs      = require('fs');
var express = require('/opt/plesk/node/7/bin/node_modules/express');
var https   = require('https');
var app     = express();

var server  = https.createServer({
    key:  fs.readFileSync('file.pem'),
    cert: fs.readFileSync('file.crt')
},app);

var io = require('/opt/plesk/node/7/bin/node_modules/socket.io').listen(server);

server.listen(8080);

---- client side ----- - - 客户端 - - -

var socket = io('/', {rejectUnauthorized: false, secure:true});

This is what the client gives me continuously:这是客户不断给我的:
https://foo.de:8080/socket.io/?EIO=3&transport=polling&t=MF9zjE6 https://foo.de:8080/socket.io/?EIO=3&transport=polling&t=MF9zjE6

Since you don't tell what error message you get it's a little hard to identify your problem.由于您不告诉您收到什么错误消息,因此很难确定您的问题。 Perhaps you should try getting your socket to work and then afterwards try to implement https.也许您应该尝试让您的套接字工作,然后再尝试实现 https。

Maybe you should try and simplify it a bit like this.也许你应该尝试像这样简化它。

Server:服务器:

var express         = require('express')
var app             = express()
var server          = require('http').Server(app)
var io              = require('socket.io')(server)

server.listen(8080);

Client:客户:

var socket = io();

This piece of code should create your socket and your express app.这段代码应该创建您的套接字和 Express 应用程序。 Then you could try and change http to https and it should work aswell.然后你可以尝试将 http 更改为 https ,它应该也能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM