同时使用ws和wss

时间:2017-04-29 12:27:09

标签: node.js http https websocket

我想在一个WebSocket服务器上同时使wswss工作。

这是我目前所拥有的,只有wss

var WebSocket = require('ws');
var https = require('https');

var privateKey = fs.readFileSync('cert/key.key');
var certificate = fs.readFileSync('cert/cert.crt');

var httpsServer = https.createServer({
    ca: ca,
    key: privateKey,
    cert: certificate
}, this.app);

var options = {
    server: httpsServer, 
    perMessageDeflate: false,
    maxPayload: 4096
};

var wss = new WebSocket.Server(options);

我知道使用Socket.io非常简单,你可以io.attach(httpServer)io.attach(httpsServer)

var httpServer = http.createServer(this.app);
var httpsServer = https.createServer({
    key: privateKey,
    cert: certificate
}, this.app);

httpServer.listen(3002, function(){
    console.log('httpServer listening on port 3002');
});

httpsServer.listen(3003, function(){
    console.log('httpsServer listening on port 3003');
});

this.io = new ioServer();
this.io.attach(httpServer);
this.io.attach(httpsServer);

使用此WebSocket库,这是否可行?

0 个答案:

没有答案