2016-02-11 25 views
7

Nie rozumiem, jak skonfigurować LEX z socket.io. Oto co mam teraz:letsencrypt-express z Socket.IO

server.js

var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 

app.use(bodyParser.urlencoded({limit: '50mb', extended: true })); 
app.use(bodyParser.json({limit: '50mb'})); 

var port = 80; 
if(process.argv[2] == 'dev') port = 3001; 

var router = express.Router(); 

app.use(function (req, res, next) { 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, userId'); 
    next(); 
}); 

var api = require('./api'); 
api(app, port) 

app.use(express.static(__dirname + "/../dist")); 
app.get('/', function(req, res) { 
    res.render('index.html'); 
}); 
console.log('Magic happens on port ' + port); 

api.js

var API = function(app, port){ 
    var pg = require('pg'); 
    var lex = require('letsencrypt-express').testing(); 

    var connectedUsers = {}; 

    var config ={ 
    user: "postgres", 
    password: "password", 
    database: "production", 
    port: 5432, 
    host: "serverIpAddress", 
    ssl: false 
    }; 

    pg.connect(config, function(err, client, done) { 

    if (err) { 
     return console.error('error fetching client from pool', err); 
    } 
    // var server = require('http').Server(app); 
    var server = lex.create({ 
     configDir: './letsencrypt.config', 
     onRequest: app, 
     letsencrypt: null, 
    }); 


    var io = require('socket.io')(server); 

    server.listen(port); 

    io.on('connection', function (socket) { 
     console.log('new connection') 

    }); 
    }); 

} 

module.exports = API 

przyjrzeć zakomentowanym linii api.js: wszystko działa dobrze, gdy używam tego zamiast od:

var server = lex.create({ 
     configDir: './letsencrypt.config', 
     onRequest: app, 
     letsencrypt: null, 
    }); 

z LEX Otrzymuję ten błąd próbując uruchomić serwer:

[LEX] testing mode turned on 
[LEX] default server: https://acme-staging.api.letsencrypt.org/directory 


################################################### 
#             # 
#  Open up a browser and visit this server  # 
#  at its domain name.       # 
#             # 
#         ENJOY!   # 
#             # 
################################################### 


Note: testing certs will be installed because .testing() was called. 
     remove .testing() to get live certs. 


[LEX] automatic registration handling turned on for testing. 


Magic happens on port 3001 
[LEX] creating sniCallback { 
    "configDir": "./letsencrypt.config", 
    "onRequest": { 
    "domain": null, 
    "_events": {}, 
    "_eventsCount": 3, 
    "_connections": 0, 
    "_handle": null, 
    "_usingSlaves": false, 
    "_slaves": [], 
    "_unref": false, 
    "allowHalfOpen": true, 
    "pauseOnConnect": false, 
    "httpAllowHalfOpen": false, 
    "timeout": 120000 
    }, 
    "letsencrypt": { 
    "backend": {} 
    }, 
    "debug": true, 
    "webrootPath": "/var/folders/2m/vfw1bgqd4pv7hk86kcbvcrlc0000gn/T/acme-challenge", 
    "privkeyPath": "./letsencrypt.config/live/:hostname/privkey.pem", 
    "fullchainPath": "./letsencrypt.config/live/:hostname/fullchain.pem", 
    "certPath": "./letsencrypt.config/live/:hostname/cert.pem", 
    "chainPath": "./letsencrypt.config/live/:hostname/chain.pem", 
    "server": "https://acme-staging.api.letsencrypt.org/directory" 
} 
projectRootFolder/node_modules/socket.io/node_modules/engine.io/lib/server.js:358 
    var listeners = server.listeners('request').slice(0); 
         ^

TypeError: server.listeners is not a function 
    at Server.attach (projectRootFolder/node_modules/socket.io/node_modules/engine.io/lib/server.js:358:26) 
    at Function.attach (projectRootFolder/node_modules/socket.io/node_modules/engine.io/lib/engine.io.js:124:10) 
    at Server.listen.Server.attach (projectRootFolder/node_modules/socket.io/lib/index.js:226:21) 
    at new Server (projectRootFolder/node_modules/socket.io/lib/index.js:51:17) 
    at Server (projectRootFolder/node_modules/socket.io/lib/index.js:39:41) 
    at projectRootFolder/server/api.js:73:34 
    at projectRootFolder/node_modules/pg/lib/pool.js:82:9 
    at projectRootFolder/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:281:11 
    at projectRootFolder/node_modules/pg/lib/pool.js:56:18 
    at null.<anonymous> (projectRootFolder/node_modules/pg/lib/client.js:149:7) 
    at g (events.js:260:16) 
    at emitOne (events.js:82:20) 
    at emit (events.js:169:7) 
    at Socket.<anonymous> (projectRootFolder/node_modules/pg/lib/connection.js:109:12) 
    at emitOne (events.js:77:13) 
    at Socket.emit (events.js:169:7) 

Chyba mam bardzo prosty błąd w moim kodzie, ponieważ wcześniej nie konfigurowałem Socket.io z SSL.

Dziękujemy

Odpowiedz

10

Spróbuj tego kodu. Działa tylko z najnowszą wersją letsencrypt-express.

app.js

var express = require('express'); 
var app = express(); 
var socketIO = require('socket.io'); 
var LEX = require('letsencrypt-express').testing(); 
var https = require('http2'); 

app.get('/', function (req, res) { 
    res.sendFile(__dirname + '/index.html'); 
}); 

var lex = LEX.create({ 
    configDir: '/etc/letsencrypt' 
, letsencrypt: null 
, approveRegistration: function (hostname, cb) { 
    cb(null, { 
     domains: ['example.com'] 
    , email: '[email protected]' 
    , agreeTos: true 
    }); 
    } 
}); 

var server = https.createServer(lex.httpsOptions, LEX.createAcmeResponder(lex, app)); 
server.listen(443); 

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

index.html

<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> 
<script> 
    var socket = io.connect('https://example.com'); 
    socket.on('news', function (data) { 
    console.log(data); 
    socket.emit('my other event', { my: 'data' }); 
    }); 
</script>