Nodejs - Express - 我无法连接到 Socket IO 对象可能是什么问题?

我只在远程服务器端遇到一个问题,这意味着即使通过 https 的自签名证书,本地也可以正常工作。但是当我将代码移动到服务器时,它也可以在本地工作,但不能远程工作。


我创建了一个节点应用程序,该应用程序托管在 https 端口 3000 下的服务器中。该应用程序具有 Socket IO 库,我已将其附加到同一个 https 服务器。我没有使用 nodeiis,因为我使用重写规则通过 Windows IIS 传递它。


我已经在 IIS 中安装了 websocket 模块。事实上,我已经在使用 websockets 而不是 Socket.IO 并且它在与我现在拥有的相同配置下运行良好,我只是用 Socket.IO 替换它,因为它更适合我的需要。


现在我的代码


网页


<script src="/socket.io/socket.io.js"></script>

<script src="/js/Client.js"></script>

客户端.js


$(document).ready(function() {


    var address = window.location.protocol + '//' + window.location.host;  

    var details = {  

       resource: (window.location.pathname.split('/').slice(0, -1).join('/') + '/socket.io').substring(1)  

    };  

    const socket = io.connect(address, details);


    socket.on('connect', function () {

        console.log('Client Connected');

        socket.emit('ping', 'hi server ping sent!');

    });


    socket.on('error', function (reason){

        console.log('Connection failed', reason); //Here is where it launch the error

    });

});

应用程序.js


....

const https = require('https');


var socketlib = require('./socketLib');

const fs = require('fs');

const app = express();

var cookieParser = require('cookie-parser');

app.use(sessionParser);

var expiryDate = new Date( Date.now() + 60 * 60 * 1000 );


const sessionParser = session({

  secret: 'secret', resave: true, cookieName: 'sessionName',

  name: 'sessionId', saveUninitialized: true,

  ephemeral: true,

  cookie: { secure: true, expires: expiryDate, SameSite : 'None',

    maxAge: 24000 * 60 * 60, // One hour

  }

});


//// HTTPS Server ////

const options = {

  key: fs.readFileSync(config.certKey),

  cert: fs.readFileSync(config.certCert)

};


var httpsServer = https.createServer(options, app, function (req, res) {

    console.log('request starting...https');

});


httpsServer.listen(3000, function(req, res) {

  console.log('Server is running at', config.nodeApiUrl + ':', port)

});


德玛西亚99
浏览 149回答 1
1回答

梵蒂冈之花

问题在于重写规则。我只是用以下内容替换了它,它就像一个魅力。&nbsp; &nbsp; &nbsp; &nbsp; <rule name="WebSocketTestRule" stopProcessing="true">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <match url=".*" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <conditions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <add input="{CACHE_URL}" pattern="domainxxx.com" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </conditions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action type="Rewrite" url="https://localhost:3000/{R:0}" />&nbsp; &nbsp; &nbsp; &nbsp; </rule>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript