Im doing my discord activity with vite as discords getting-
started tutorial. I created ws server via Express and Socket.io, in browser it works correctly, but when i launch it in discord it doesn’t work correctly.
client/main.js
import { DiscordSDK } from "@discord/embedded-app-sdk";
import { io } from "socket.io-client";
import "./style.css";
// Will eventually store the authenticated user's access_token
// let auth;
// let VITE_DISCORD_CLIENT_ID = 'DS_APP_CLIENT_ID';
// const discordSdk = new DiscordSDK(VITE_DISCORD_CLIENT_ID);
//
// setupDiscordSdk().then(() => {
// console.log("Discord SDK is authenticated");
//
// // We can now make API calls within the scopes we requested in setupDiscordSDK()
// // Note: the access_token returned is a sensitive secret and should be treated as such
// });
async function setupDiscordSdk() {
await discordSdk.ready();
console.log("Discord SDK is ready");
// Authorize with Discord Client
const { code } = await discordSdk.commands.authorize({
client_id: VITE_DISCORD_CLIENT_ID,
response_type: "code",
state: "",
prompt: "none",
scope: [
"identify",
"guilds",
],
});
// Retrieve an access_token from your activity's server
const response = await fetch("/api/token", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
code,
}),
});
const { access_token } = await response.json();
// Authenticate with Discord client (using the access_token)
auth = await discordSdk.commands.authenticate({
access_token,
});
if (auth == null) {
throw new Error("Authenticate command failed");
}
document.querySelector('#app').innerHTML = `
<div>
<img src="https://cdn.discordapp.com/avatars/${auth.user.id}/${auth.user.avatar}" class="logo" alt="Discord" />
<h1>Hello, World! ${auth.user.global_name}</h1>
</div>
`;
}
const socket = io('https://ws-server-url.tunneled-by-cloudflared')
socket.on('new_connection', (val) => {
console.log(`New connection connected ${socket.id}`);
let p = document.createElement('p');
p.innerText = val;
document.querySelector('#app').innerHTML = val;
})
ws-server/server.js
import express from "express";
import dotenv from "dotenv";
import { Server } from "socket.io";
dotenv.config({ path: "../.env" });
const app = express();
const port = 3002;
app.use(express.json());
const server = app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
const wsServer = new Server(server, {
cors: {
origin: "*"
}
});
wsServer.on("connection", (socket) => {
wsServer.emit('new_connection', socket.id)
console.log(`New connection connected ${socket.id}`);
})
Can you help me pls?
I tried to find discord activity console but I didn’t find it/