diff --git a/worker/TldrawDurableObject.ts b/worker/TldrawDurableObject.ts index 79640ef..7bf5ce0 100644 --- a/worker/TldrawDurableObject.ts +++ b/worker/TldrawDurableObject.ts @@ -120,32 +120,29 @@ export class TldrawDurableObject { // what happens when someone tries to connect to this room? async handleConnect(request: IRequest): Promise { - // extract query params from request const sessionId = request.query.sessionId as string if (!sessionId) return error(400, 'Missing sessionId') - // Create the websocket pair for the client const { 0: clientWebSocket, 1: serverWebSocket } = new WebSocketPair() - // @ts-ignore serverWebSocket.accept() - // load the room, or retrieve it if it's already loaded const room = await this.getRoom() - - // connect the client to the room room.handleSocketConnect({ sessionId, socket: serverWebSocket }) - // return the websocket connection to the client + const origin = request.headers.get('Origin') || '*' + return new Response(null, { status: 101, webSocket: clientWebSocket, headers: { - 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Origin': origin, 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, UPGRADE', 'Access-Control-Allow-Headers': '*', - 'Access-Control-Allow-Credentials': 'true' + 'Access-Control-Allow-Credentials': 'true', + 'Upgrade': 'websocket', + 'Connection': 'Upgrade' } - }); + }) } getRoom() { diff --git a/worker/worker.ts b/worker/worker.ts index 00456a0..b51aee2 100644 --- a/worker/worker.ts +++ b/worker/worker.ts @@ -76,7 +76,11 @@ const router = AutoRouter({ .get('/connect/:roomId', (request, env) => { const id = env.TLDRAW_DURABLE_OBJECT.idFromName(request.params.roomId) const room = env.TLDRAW_DURABLE_OBJECT.get(id) - return room.fetch(request.url, { headers: request.headers, body: request.body }) + return room.fetch(request.url, { + headers: request.headers, + body: request.body, + method: request.method + }) }) // assets can be uploaded to the bucket under /uploads: