简体   繁体   中英

node.js socket io https

I'm trying to establish a WebSocket (with node.js/socket.io) on an HTTPS protocol. 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?

---- 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

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.

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. Then you could try and change http to https and it should work aswell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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