简体   繁体   English

我在 Heroku 上部署应用程序时遇到了 Socket io 问题

[英]I got a problem with Socket io when I deployed my application on Heroku

I created a chat room using socket.io within my express app and it's working fine in local host when I deployed my application to heroku I get this error when I open the chat room我在我的 express 应用程序中使用 socket.io 创建了一个聊天室,当我将应用程序部署到 heroku 时,它在本地主机上运行良好 我打开聊天室时收到此错误

GET http://localhost:5000/socket.io/?EIO=3&transport=polling&t=N1PV-tt net::ERR_CONNECTION_REFUSED

here is my index.js file这是我的 index.js 文件

var express = require("express")
var cors = require("cors")
var bodyParser = require("body-parser")
var app = express()
var mongoose = require("mongoose")
//var http = require('http').createServer(app);
var io = require('socket.io')(http)
var port = process.env.PORT || 5000
var server = app.listen(port);
var io = require('socket.io').listen(server);
var http = require('http');

let users = [];
let messages = [];
let index = 0;

app.use(bodyParser.json())
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))

const mongoURI = 'mongodb+srv://1920:1920@cluster0-qyzs9.mongodb.net/liebherr?retryWrites=true&w=majority'

mongoose.connect(mongoURI, { useNewUrlParser: true })
    .then(() => console.log("MongoDB Connected"))
    .catch(err => console.log(err))

var Users = require("./routes/Users")

app.use("/users", Users)


//Socket connection

io.on('connection', (socket) => {
    io.emit('noOfConnections', Object.keys(io.sockets.connected).length)


    socket.on('disconnect', () => {
        console.log(`a user has left the chat.`);
        io.emit('noOfConnections', Object.keys(io.sockets.connected).length)
    })



    socket.on('chat-message', (msg) => {
        socket.broadcast.emit('chat-message', msg)
    })
    socket.on('typing', (data) => {
        socket.broadcast.emit('typing', data)
    })
    socket.on('stoptyping', () => {
        socket.broadcast.emit('stoptyping')
    })
})

    // Static folder
    app.use(express.static(__dirname + '/public/'));
    app.get(/.*/);


and here is package.json file这是 package.json 文件

{
  "name": "express-demo",
  "version": "1.0.0",
  "description": "This is a full stack application",
  "engines": {
    "node": "13.1.0"
  },
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "author": "Farouk Turki",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^3.0.7",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongodb": "^3.5.2",
    "mongoose": "^5.8.9",
    "nodemon": "^2.0.2",
    "socket.io": "^2.3.0"
  }
}

Everything is working fine except the chatroom so I need to know why it's still using port 5000 in localhost除了聊天室,一切正常,所以我需要知道为什么它仍然在本地主机中使用端口 5000

From the front end (socket.io client) you are setting the hostname as localhost .从前端(socket.io 客户端),您将主机名设置为localhost But a front end code is not run server-side, it is in client side(your browser/or any thin client), it can access only the resources that are accessible over the internet(or the resources which is accessible by the browser itself).但是前端代码不在服务器端运行,它在客户端(您的浏览器/或任何瘦客户端),它只能访问可通过互联网访问的资源(或浏览器本身可访问的资源) )。

So, in your case, the connection code will look something like:因此,在您的情况下,连接代码将类似于:

<script>
  const socket = io('http://<your-heroku-public-url-for-the-express-server>');
  // which typically is 
  const socket = io('http://randomname.herokuapp.com');
</script>

Given that http://randomname.herokuapp.com is the public url of your node js server.鉴于http://randomname.herokuapp.com是您的节点 js 服务器的公共 url。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署到heroku时,如何使我的node / socket.io应用使用正确的端口? - How do I get my node/socket.io app to use the correct port when deployed to heroku? 当我将我的应用程序部署到 heroku 时,socket.io 不起作用 - socket.io not working when i deploy my app to heroku Socket.io 应用程序未部署到 Heroku - Socket.io application is not getting deployed to Heroku 在 docker 中运行 socket.io 时出现此错误 - I got this error when run socket.io in docker 当我在heroku上部署我的nodejs应用程序时,构建成功但出现了“应用程序错误” - When I deploy my nodejs app on heroku, build success but got a “Application Error” 使用最新版本的 socket.io 时,我的 cors 出现问题 - I have a problem with my cors when using the latest version of socket.io 当我在 heroku 中部署我的应用程序时,获取 html 时出错 - Error getting html when I deployed my app in heroku 当我打开我的 heroku 应用程序时,我收到了这条消息? - When i run open my heroku app i got this meessage? 我应该为我的nodejs应用程序使用socket.io吗? - Should I be using socket.io for my nodejs application? 我的节点应用程序在 localhost 上运行良好,但是当我部署在 heroku 上时,它给了我应用程序错误。 你可以阅读下面的描述 - my node app is working fine on localhost but when i deployed on heroku , it is giving me application error. you can read description below
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM