下面是我的套接字服务器应用程序的代码。当套接字客户端托管在我自己的服务器上时,它正在连接到服务器应用程序,但是托管在客户端服务器上的却没有。我不明白为什么会发生这种情况
当托管在客户端服务器上时,它会继续尝试连接到套接字服务器应用程序,但从不连接
const mongo = require('./modules/db.js')
const fs = require('fs')
const cors = require('cors')
let db
const dbCheck = setInterval(() => {
const x = mongo.pending() // check if any database is pending to be connected to or not
if (!x) {
console.log('\x1b[32m%s\x1b[0m', "[Oniv real-time] No databse pending to be connected to. Starting real-time server")
clearInterval(dbCheck)
start()
}
}, 10)
function start () {
console.log('\x1b[34m%s\x1b[0m', "[Oniv real-time] Loading modules")
const express=require('express')
const app = express()
const fs = require("fs")
const socket = require('./modules/main.js').socketServer
app.use(cors())
console.log('\x1b[34m%s\x1b[0m', "[Oniv real-time] Initializing real-time server")
let http, io, httpsServer, certs = false
if (fs.readdirSync('./../').indexOf('certs') > -1) {
certs = true
}
if(process.env.NODE_ENV === "development"){
http = require('http').Server(app)
io = require('socket.io')(http)
} else {
const credentials = {
key: fs.readFileSync('./../certs/privateKey.key'),
cert: fs.readFileSync('./../certs/serverCert.crt'),
ca: [
// fs.readFileSync('./../certs/SecureServerCA.crt'),
fs.readFileSync('./../certs/AddTrustCA.crt')
]
}
http = require('https').Server(credentials, app)
io = require('socket.io')(http)
}
io.on('connection', socket)
const port = 8888
http.listen(port,function(){
console.log('\x1b[42m%s\x1b[0m', "[Oniv real-time] listening on port " + port)
})
module.exports.io = function () {
return io
}
}