Pepguy commited on
Commit
2fe2d50
·
verified ·
1 Parent(s): 003dc51

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +7 -26
app.js CHANGED
@@ -1,5 +1,5 @@
 
1
  const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
2
- const HARDCODED_TOKEN = "test-token-123";
3
 
4
  Bun.serve({
5
  port: Number(Bun.env.PORT) || 7860,
@@ -14,7 +14,7 @@ Bun.serve({
14
  headers: {
15
  "Access-Control-Allow-Origin": "*",
16
  "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
17
- "Access-Control-Allow-Headers": "Content-Type, Upgrade, Authorization",
18
  "Access-Control-Allow-Credentials": "true",
19
  },
20
  });
@@ -22,11 +22,6 @@ Bun.serve({
22
 
23
  // Upgrade to WebSocket if requested
24
  if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
25
- // Bearer token check
26
- const auth = req.headers.get("authorization");
27
- if (auth !== `Bearer ${HARDCODED_TOKEN}`) {
28
- return new Response("Unauthorized", { status: 401, headers: { "Access-Control-Allow-Origin": "*" } });
29
- }
30
  // Accept the upgrade with CORS headers on the handshake
31
  const upgradeRes = server.upgrade(req, {
32
  headers: {
@@ -52,28 +47,15 @@ Bun.serve({
52
  <h2>Join a Room & Send Messages</h2>
53
  <input id="room" placeholder="Room ID"/>
54
  <button onclick="joinRoom()">Join Room</button>
55
- <div>
56
- <input type="checkbox" id="sendToken" checked>Send Bearer Token
57
- </div>
58
  <div id="log"></div>
59
  <input id="msg" placeholder="Type a message" style="width:80%;"/>
60
  <button onclick="sendMsg()">Send</button>
61
  <script>
62
  var ws, currentRoom;
63
- function connectWS() {
64
  var scheme = location.protocol === 'https:' ? 'wss' : 'ws';
65
  var socketUrl = scheme + '://' + location.host + '/';
66
- var sendToken = document.getElementById('sendToken').checked;
67
- if (ws && ws.readyState === 1) ws.close();
68
- // Bun supports custom headers in WebSocket constructor, browsers do not!
69
- try {
70
- ws = sendToken
71
- ? new WebSocket(socketUrl, { headers: { "Authorization": "Bearer test-token-123" } })
72
- : new WebSocket(socketUrl);
73
- } catch (e) {
74
- log('Custom headers not supported in this environment.');
75
- ws = new WebSocket(socketUrl);
76
- }
77
  ws.onopen = function(){ log('🔌 Connected'); };
78
  ws.onmessage = function(ev){
79
  var m = JSON.parse(ev.data);
@@ -81,9 +63,7 @@ Bun.serve({
81
  };
82
  ws.onerror = function(){ log('⚠️ WebSocket error'); };
83
  ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
84
- }
85
- window.addEventListener('load', connectWS);
86
- document.getElementById('sendToken').addEventListener('change', connectWS);
87
  function joinRoom(){
88
  var id = document.getElementById('room').value.trim();
89
  if(!id) return alert('Enter room ID');
@@ -157,4 +137,5 @@ Bun.serve({
157
  }
158
  });
159
 
160
- console.log("✅ Bun realtime server running on port " + (Bun.env.PORT || 7860));
 
 
1
+ // server.js (Bun) — with CORS enabled for WS and HTTP
2
  const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
 
3
 
4
  Bun.serve({
5
  port: Number(Bun.env.PORT) || 7860,
 
14
  headers: {
15
  "Access-Control-Allow-Origin": "*",
16
  "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
17
+ "Access-Control-Allow-Headers": "Content-Type, Upgrade",
18
  "Access-Control-Allow-Credentials": "true",
19
  },
20
  });
 
22
 
23
  // Upgrade to WebSocket if requested
24
  if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
 
 
 
 
 
25
  // Accept the upgrade with CORS headers on the handshake
26
  const upgradeRes = server.upgrade(req, {
27
  headers: {
 
47
  <h2>Join a Room & Send Messages</h2>
48
  <input id="room" placeholder="Room ID"/>
49
  <button onclick="joinRoom()">Join Room</button>
 
 
 
50
  <div id="log"></div>
51
  <input id="msg" placeholder="Type a message" style="width:80%;"/>
52
  <button onclick="sendMsg()">Send</button>
53
  <script>
54
  var ws, currentRoom;
55
+ window.addEventListener('load', function() {
56
  var scheme = location.protocol === 'https:' ? 'wss' : 'ws';
57
  var socketUrl = scheme + '://' + location.host + '/';
58
+ ws = new WebSocket(socketUrl);
 
 
 
 
 
 
 
 
 
 
59
  ws.onopen = function(){ log('🔌 Connected'); };
60
  ws.onmessage = function(ev){
61
  var m = JSON.parse(ev.data);
 
63
  };
64
  ws.onerror = function(){ log('⚠️ WebSocket error'); };
65
  ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
66
+ });
 
 
67
  function joinRoom(){
68
  var id = document.getElementById('room').value.trim();
69
  if(!id) return alert('Enter room ID');
 
137
  }
138
  });
139
 
140
+ console.log("✅ Bun realtime server running on port " + (Bun.env.PORT || 7860));
141
+