8

socket.io でLEXを構成する方法がわかりません。ここに私が今持っているもの:

サーバー.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

api.js のコメントアウトされた行を見てください: 代わりにこれを使用すると、すべて正常に動作します:

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

LEX を使用すると、サーバーを実行しようとすると次のエラーが表示されます。

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

以前は SSL を使用して Socket.io を構成していなかったので、コードに非常に単純な間違いがあると思います。

ありがとうございました

4

1 に答える 1

11

このコードを試してください。これは、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: 'info@example.com'
    , 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>
于 2016-02-15T20:04:14.643 に答える