Skip to content

Commit

Permalink
ws and wss fix
Browse files Browse the repository at this point in the history
  • Loading branch information
GitGinocchio committed Feb 23, 2025
1 parent f59d347 commit 03a4f70
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/ext/syncify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const reconnectionAttempts = 3;
const Addresses = [
`http://127.0.0.1:8787`,
`https://syncify.ginocchio.workers.dev/`
`https://syncify.ginocchio.workers.dev`,
`http://127.0.0.1:8787`
];

let customButton;
Expand Down Expand Up @@ -81,7 +81,7 @@ function showDialog(title, message) {

async function attemptConnection(url, user_data) {
return new Promise((resolve, reject) => {
socket = new WebSocket(`${url.replace("https", "ws").replace("http", "ws")}/auth/${user_data.id}`);
socket = new WebSocket(`${url.replace("https", "wss").replace("http", "ws")}/auth/${user_data.id}`);

socket.addEventListener("open", (event) => {
const data = JSON.stringify({
Expand Down
4 changes: 2 additions & 2 deletions src/web/durables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class Room extends DurableObject {
if (!this.awakaned) { await this.awake(); }

return {
id : this.id,
id : this.id.toString(),
name : this.name,
max_members : this.max_members,
editable_queue : this.editable_queue,
Expand Down Expand Up @@ -400,7 +400,7 @@ export class User extends DurableObject {
if (!this.awakaned) { await this.awake(); }

return {
id : this.id,
id : this.id.toString(),
spotifyid : this.spotifyid,
display_name : this.display_name,
birthdate : this.birthdate,
Expand Down
2 changes: 1 addition & 1 deletion src/web/routes/403/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Auth from '../../auth.js';
import Utils from '../../utils.js';

export default {
async get (request, env, ctx) {
async get (request : Request, env, ctx) {
const url = new URL(request.url);
const cookies = Utils.parseCookies(request.headers.get('cookie'));

Expand Down
5 changes: 3 additions & 2 deletions src/web/routes/404/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Page404 from './404.html';
import mustache from 'mustache';

import { User } from '../../durables.js';
import Auth from '../../auth.js';
import Utils from '../../utils.js';

Expand All @@ -13,10 +14,10 @@ export default {
const token = cookies.get('user_access_token');
const payload = await Auth.verifyToken(token, env.JWT_SECRET_KEY);

let data = null;
let data : any = null;
if (payload != null){
const id = env.users.idFromString(payload.id);
const user = env.users.get(id);
const user : User = env.users.get(id);
data = await user.getData();
}

Expand Down
2 changes: 1 addition & 1 deletion src/web/routes/all/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


export default {
async get (request, env, ctx) {
async get (request : Request, env, ctx) {
const url = new URL(request.url);
url.pathname = '/404'
return Response.redirect(url);
Expand Down
12 changes: 7 additions & 5 deletions src/web/routes/bugreport/route.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
// @ts-ignore
import BugReport from './bugreport.html'
import mustache from 'mustache';

import { User } from '../../durables.js';
import Auth from '../../auth.js'
import Utils from '../../utils.js';

export default {
async post (request, env, ctx) {
async post (request : Request, env, ctx) {
return new Response("Not implemented");
},

async get (request, env, ctx) {
async get (request : Request, env, ctx) {
const cookies = Utils.parseCookies(request.headers.get('cookie'));
const url = new URL(request.url);

const token = cookies.get('user_access_token');
const payload = await Auth.verifyToken(token, env.JWT_SECRET_KEY);

let data = null;
let data : any = null;
if (payload != null){
const id = env.users.idFromString(payload.id);
const user = env.users.get(id);
data = await user.getUserData();
const user : User = env.users.get(id);
data = await user.getData();
}

const html = mustache.render(BugReport, {
Expand Down
5 changes: 3 additions & 2 deletions src/web/routes/index/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export default {
const token = cookies.get('user_access_token');
const payload = await Auth.verifyToken(token, env.JWT_SECRET_KEY);

if (payload) {
return Response.redirect(`${url.protocol}${url.hostname}:${url.port}/user`);
if (payload) {
url.pathname = '/user'
return Response.redirect(url);
}

return new Response(Index, { headers: { 'Content-Type': 'text/html' }});
Expand Down
4 changes: 2 additions & 2 deletions src/web/routes/logout/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
async get (request, env, ctx) {
async get (request : Request, env, ctx) {
const url = new URL(request.url);

return new Response(null, {
headers: {
'Set-Cookie': `user_access_token=; Max-Age=-1; room_access_token=; Max-Age=-1;`,
'Set-Cookie': `user_access_token=; Path=/; Max-Age=0; Secure; HttpOnly; room_access_token=; Path=/; Max-Age=0; Secure; HttpOnly;`,
Location: `/`
},
status: 302
Expand Down
2 changes: 1 addition & 1 deletion src/web/routes/onboard/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import OnBoard from './onboard.html'
import mustache from 'mustache';

export default {
async get (request, env, ctx) {
async get (request : Request, env, ctx) {
const url = new URL(request.url);

/*
Expand Down
2 changes: 1 addition & 1 deletion src/web/routes/room/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function copyurl(roomid) {
}

document.addEventListener("DOMContentLoaded", (event) => {
const socket = new WebSocket(`ws://${window.location.host}/room`);
const socket = new WebSocket((window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + '/room');

const messageInput = document.getElementById("message-input");
const queueInput = document.getElementById("queue-input");
Expand Down
2 changes: 1 addition & 1 deletion src/web/routes/room/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default {
url: user_data.external_urls.spotify
},
room : {
id : room_id,
id : room_id.toString(),
chat : room_data.messages,
members : room_data.members.values(),
artists : room_data.artists,
Expand Down

0 comments on commit 03a4f70

Please sign in to comment.