【问题标题】:socket.io not working on herokusocket.io 无法在 heroku 上运行
【发布时间】:2021-05-16 20:16:43
【问题描述】:

每当我尝试在 heroku 上使用 require("socket.io"); 时,它都会失败并显示消息“找不到模块 socket.io”。

我认为这是我的设置问题,因为在我的本地 node.js 服务器上运行良好。

我需要改变什么?

【问题讨论】:

标签: node.js heroku


【解决方案1】:

Cedar 上的 Heroku 不支持 websockets

无论如何你都可以使用socket.io

io.set("transports", ["xhr-polling"]); 
io.set("polling duration", 10); 

https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku

【讨论】:

    【解决方案2】:

    您必须更改您的 PaaS 提供商。 Heroku 不支持 websocket。 nodejitsu 以支持 websockets 而闻名。

    另外,您可能忘记在 package.json 依赖列表中添加 socket.io

    【讨论】:

    【解决方案3】:

    package.json 修改为

    "dependencies": {
    "async":     "0.1.18",
    "ejs":       "0.4.3",
    "express":   "2.4.6",
    "faceplate": "0.0.4",
    "socket.io": "latest"   },
    

    而服务器端代码是:

    var port=process.env.PORT || 3000;
    var http=require('http');
    var app=http.createServer(function(req,res){
        res.write("server listening to port:"+port);
        res.end();
    }).listen(port);
    socket=require("socket.io");
    io=socket.listen(app);
    io.configure(function () { 
      io.set("transports", ["xhr-polling"]); 
      io.set("polling duration", 10); 
    });
    io.sockets.on("connection",function(socket){
        console.log("new connection");
        socket.on("eventA",function(data){
            io.sockets.emit("eventB",data);
        }); 
    });
    

    像魅力一样工作!!!

    【讨论】:

      【解决方案4】:

      根据this project wiki page,您需要将 socket.IO 上的传输选项更改为持续时间为 (10) 秒的 xhr-polling。

      【讨论】:

        【解决方案5】:

        websocket 传输正在以 beta 状态在 Heroku 上运行。您可以使用heroku labs:enable websockets -a YOUR_APP_NAME 启用它

        【讨论】:

          【解决方案6】:

          这些是socket.io相关问题的解决方案

          我希望我会工作

          1. (index.js 或 server.js)和(index.html 和您的 client.js)端口中的端口必须不同。 (参考下面的代码)

          =============你的 index.js 文件 ======================

          (这里的端口是8000)

          const express = require("express")
          var app = express();
          const http = require('http')
          var server = http.createServer(app);
            
          const port = process.env.PORT || 8000
          server.listen(port,()=>
          {
              console.log("Listening at port => "+port)
          });
          var io = require('socket.io')(server, {
              cors: {
                origin: '*',
              }
          });
          
          const cors = require("cors")
          app.use(cors()) 
          

          =============你的client.js文件======================

          这里的端口是 8080

          const socket = io.connect('https://localhost:8080/')
          

          =============您的 index.html 文件 ======================

          这里的端口是 8080

           <script defer src="https://localhost:8080/socket.io/socket.io.js"> 
           </script>
          

          请记住您的“server.js 或 index.js”端口应该与“client.js”端口不同(记住这一点很重要)

          (index.html 和你的 client.js)端口必须相同

          1. 在使用 socket.io 时,您应该始终使用“http”(参考上面的代码)

          2. U 可能不包含 cors,因为它允许你拥有更多资源,没有 cors heroku 会阻止某些依赖项无法安装在 heroku 中(请参阅上面的代码)

          3. 尝试将“io”替换为“io.connect”

            const socket = io.connect('https://localhost:8080/')

          4. 必须在HTML末尾写标签

          5. 你可能会忘记添加这个必须在“socket.io”中的代码

          在您的 html 文件中是必需的

          1. 删除“node_modules”和“package-lock.json” 并在 cmd 中写入“npm i”

          2. 这应该在 package.json 的脚本中

            "开始":"节点 index.js",

          我不是在说 nodemon ,这里使用简单的节点

          1. 可能是版本造成了问题,你可以通过将所有“devDependencies”复制到“package.json”中的“dependencies”并将“*”放在这样的版本中来避免它

            “依赖”:{

            "cors": "*",

            “快递”:“*”,

            "nodemon": "*",

            “socket.io”:“*”

            },

            “devDependencies”:{}

          【讨论】:

            猜你喜欢
            • 2017-07-01
            • 2020-06-28
            • 2015-10-20
            • 2022-01-21
            • 1970-01-01
            • 2015-01-25
            • 1970-01-01
            • 1970-01-01
            • 2018-10-17
            相关资源
            最近更新 更多