为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

德玛西亚99 2022-06-05 09:47:03
我只在远程服务器端遇到一个问题,这意味着即使通过 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)});
查看完整描述

1 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

问题在于重写规则。我只是用以下内容替换了它,它就像一个魅力。


        <rule name="WebSocketTestRule" stopProcessing="true">

            <match url=".*" />

            <conditions>

                <add input="{CACHE_URL}" pattern="domainxxx.com" />

            </conditions>

            <action type="Rewrite" url="https://localhost:3000/{R:0}" />

        </rule> 


查看完整回答
反对 回复 2022-06-05
  • 1 回答
  • 0 关注
  • 148 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号