Fix formatting issue in wrangler.jsonc by removing extra newline

This commit is contained in:
Insub Kim 2025-11-17 00:57:06 +09:00
parent 0240306058
commit 7509f3d23c
2 changed files with 1 additions and 117 deletions

View File

@ -1,115 +0,0 @@
import { DurableObject } from 'cloudflare:workers';
export interface Env {
COUNTER: DurableObjectNamespace;
}
interface Session {
id: string;
webSocket: WebSocket;
quit?: boolean;
}
export class CounterDurableObject extends DurableObject {
private sessions: Map<WebSocket, Session>;
private count: number;
private lastUpdate: number;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.sessions = new Map();
this.count = 0;
this.lastUpdate = Date.now();
// Durable Objects에서 영구 저장소로부터 카운트를 복원
this.ctx.blockConcurrencyWhile(async () => {
const stored = await this.ctx.storage.get<number>('count');
if (stored !== undefined) {
this.count = stored;
}
});
}
async fetch(request: Request): Promise<Response> {
// WebSocket 업그레이드 요청 처리
const upgradeHeader = request.headers.get('Upgrade');
if (!upgradeHeader || upgradeHeader !== 'websocket') {
return new Response('Expected Upgrade: websocket', { status: 426 });
}
// WebSocketPair 생성
const webSocketPair = new WebSocketPair();
const [client, server] = Object.values(webSocketPair);
// 세션 생성
const session: Session = {
id: crypto.randomUUID(),
webSocket: server
};
// WebSocket Hibernation API 사용
this.ctx.acceptWebSocket(server);
this.sessions.set(server, session);
// 현재 상태 전송
this.broadcast();
return new Response(null, {
status: 101,
webSocket: client
});
}
async webSocketMessage(ws: WebSocket, message: ArrayBuffer | string) {
try {
const data = typeof message === 'string' ? JSON.parse(message) : null;
if (data && data.type === 'increment') {
// 카운트 증가
this.count++;
this.lastUpdate = Date.now();
// 영구 저장소에 저장
await this.ctx.storage.put('count', this.count);
// 모든 클라이언트에 브로드캐스트
this.broadcast();
} else if (data && data.type === 'reset') {
// 카운트 리셋
this.count = 0;
this.lastUpdate = Date.now();
await this.ctx.storage.put('count', this.count);
this.broadcast();
}
} catch (error) {
console.error('Error handling message:', error);
}
}
async webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean) {
// 세션 제거
this.sessions.delete(ws);
ws.close(code, 'Durable Object is closing WebSocket');
// 남은 클라이언트들에게 업데이트 전송
this.broadcast();
}
private broadcast() {
const message = JSON.stringify({
count: this.count,
online: this.sessions.size,
lastUpdate: this.lastUpdate
});
// 모든 연결된 WebSocket에 메시지 전송
this.ctx.getWebSockets().forEach((ws) => {
try {
ws.send(message);
} catch (error) {
console.error('Error broadcasting to client:', error);
}
});
}
}

View File

@ -22,4 +22,3 @@
}
]
}