socket.io
socket.io copied to clipboard
Getting "WebSocket connection to wss failed:" and not working
Describe the bug I've set up a socket.io on my server and I'm connecting to it from my other projects. Actually everything was working great on localhost. Then when I deployed it to the server, it started showing the error you see in the title and the picture below, that is, it could not connect to the server. I am using Cloudways. They have created a ticket and will review it. Anyone have experience with this?
To Reproduce
Socket.IO server version: x4.7.2
Server
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const multipleChain = require("./multiplechain");
const fs = require('fs');
const mainSite = "https://beycanpress.com/";
(async () => {
let result;
try {
result = (await fetch(
mainSite + "wp-json/licensor-api/active-license-domains"
).then(res => res.json())).data;
if (!result.success) {
result = [];
}
} catch (error) {
result = [];
}
let origins = result.map((origin, index) => {
return 'https://' + origin;
});
origins = origins.concat(require('./my-domains.json'));
// const credentials = {
// key: fs.readFileSync('./key.pem', 'utf8'),
// cert: fs.readFileSync('./cert.pem', 'utf8')
// };
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: origins
}
});
const receiverIdPair = {};
const oldTransactions = {};
io.on("connection", (socket) => {
socket.join(socket.id);
console.log('Connected: ' + socket.id);
socket.on('start-listen-transaction', async (data) => {
let receiver = data.receiver;
let tokenAddress = data.tokenAddress;
let receiver1 = receiver.toLowerCase();
receiverIdPair[socket.id] = receiver1;
if (!oldTransactions[receiver1]) {
oldTransactions[receiver1] = [];
}
let config;
if (data.network.code == 'evmBased') {
config = {
network: JSON.parse(JSON.stringify(data.network)),
};
} else {
config = {
network: data.network.code
};
}
config.testnet = data.testnet;
config.customRpc = data.customRpc;
config.customWs = data.customWs;
const provider = multipleChain.selectNetwork(config);
provider.listenTransactions({
receiver,
tokenAddress,
}, async (sub, tx) => {
let amount = await tx.getTransferAmount({
receiver,
tokenAddress,
});
if (!oldTransactions[receiver1].includes(tx.hash) && amount == data.paymentPrice) {
sub.unsubscribe();
if (!oldTransactions[receiver1].includes(tx.hash)) {
oldTransactions[receiver1].push(tx.hash);
}
io.to(socket.id).emit('new-transaction', tx.hash);
}
});
});
socket.on('disconnect', function() {
let receiver = receiverIdPair[socket.id];
delete receiverIdPair[socket.id];
if (!Object.values(receiverIdPair).includes(receiver)) {
delete oldTransactions[receiver];
}
});
});
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.get("/get-origins", (req, res) => {
res.send(origins);
});
app.get("/version", (req, res) => {
res.send("6");
});
httpServer.listen(3000, () => {
console.log("listening on *:3000");
});
})();
Socket.IO client version: 4.7.2
Client
import { io } from "socket.io-client";
const socket = io("https://qr-verifier.beycanpress.com/");
socket.emit('start-listen-transaction', {
tokenAddress,
receiver: this.receiver,
network: this.selectedNetwork,
testnet: this.provider.testnet,
paymentPrice: this.order.paymentPrice,
customWs: this.$store.getters.getCustomWs,
customRpc: this.$store.getters.getCustomRpc,
});
socket.on('new-transaction', (hash) => {
this.hash = hash;
return resolve('success');
})
Expected behavior To fix this problem and websocket to work smoothly.
Additional context