0
\$\begingroup\$

I have a WebSocket hosting on repl.it

But when the repl sleeps, the WebSocket does as well.

My friends told me to have a pinger to ping the site. It doesn't work.

They told me to fetch the server.

That doesn't work either, because when the repl sleeps, CORS is disabled although my code enables CORS.

What do I do?

SERVER CODE (NODEJS):

const express = require('express');
const fs = require("fs");
const path = require('path');
const cors = require('cors')
const app = express();
const server = require('http').createServer(app);
const WebSocket = require("ws")
const wss = new WebSocket.Server({ server })

app.use(cors())
app.use(express.json());
app.use(express.static('release'));
app.use('/assets', express.static('assets'));

app.use((_, res) => {
    res.status(404).sendFile(path.join(__dirname, '/error/404.html'));
});

wss.on('connection', ws => {
    fs.readFile("highscore.txt", 'utf-8', (err, val) => {
        if (err) throw err;
        wss.clients.forEach(client => {
            if (client === ws) {
                client.send(`I ${val}`)
            }
        })
        console.log(val);
    });

    console.log('socket connected!');

    ws.on("message", msg => {
        let msgs = msg.split(" ");
        let pre = msgs[0]
        if (pre == "R") {
            fs.readFile("highscore.txt", 'utf-8', (err, val) => {
                let score = msgs[1]
                if (err) throw err;
                if (val < score) {
                    console.log(`There is a new score! That is:${score}`);
                    fs.writeFile('highscore.txt', score, (err) => { })
                    wss.clients.forEach(client => {
                        client.send(`U ${score}`)
                    })
                };
            });
        }
    });
});

server.listen(2195, () => {
    console.log(`server running`);
});

CLIENT CODE

import { loadProgress } from './main'
let socket;

function init(details) {
    fetch("https://space-chaos--coder2195text.repl.co")
        .then(response => {
            return response.text();
        })
        .then(final => {
            console.log(final)
            loadProgress.update("ws", 100)
            socket = new WebSocket("wss://space-chaos--coder2195text.repl.co/")
            socket.onmessage = (msg) => {
                let msgs = msg.data.split(" ")
                if (msgs[0] === "U") {
                    details.update(msgs[1])
                } else if (msgs[0] === "I") {
                    details.init(msgs[1])
                }
            }
        })
}

function requestPush(score) {
    let t = setInterval(() => {
        if (socket) {
            if (socket.readyState === 1) {
                socket.send(`R ${score}`);
                clearInterval(t)
            }
        }
    }, 100)
}

export { requestPush, init };
```
\$\endgroup\$
8
  • \$\begingroup\$ It sounds like you might want to upgrade to the always-on version at $7/month if you want always-on functionality. \$\endgroup\$ Commented Jun 2, 2021 at 17:11
  • \$\begingroup\$ ok imma causally pull money out of thin air... that's not a solution -_- \$\endgroup\$ Commented Jun 2, 2021 at 17:12
  • \$\begingroup\$ Can I perhaps use iframes? \$\endgroup\$ Commented Jun 3, 2021 at 14:44
  • \$\begingroup\$ Hard to say without more details about your setup. We know you're using WebSocket, we don't know how you're using it or what you're using it for. We know you tried a ping, but we don't know how you tried a ping. We know you tried to "fetch the server", but we don't know how you tried doing that. A vague question tends to attract only vague answers, or none at all. \$\endgroup\$ Commented Jun 3, 2021 at 14:46
  • \$\begingroup\$ iframe I could make the repl boot up, and once the iframe is loaded, I can guarantee the socket has opened. or I can keep fetching until the server boots up, because I noticed even though a CORS error occurred, the server booted up \$\endgroup\$ Commented Jun 3, 2021 at 14:48

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.