SlnDmry commited on
Commit
7c8f1a1
·
verified ·
1 Parent(s): 293080e

Update bot.js

Browse files
Files changed (1) hide show
  1. bot.js +18 -48
bot.js CHANGED
@@ -393,7 +393,6 @@ app.get("/status", (req, res) => {
393
 
394
  app.get("/", (req, res) => res.send("Bot Aktif"));
395
 
396
- // Görüntüleyiciyi tetikleyen middleware
397
  const checkAndStartViewer = (req, res, next) => {
398
  if (viewerTimeout) {
399
  clearTimeout(viewerTimeout);
@@ -404,11 +403,9 @@ const checkAndStartViewer = (req, res, next) => {
404
 
405
  if (!viewerInstance && isBotReady) {
406
  try {
407
- mineflayerViewer(bot, { port: 3000, firstPerson: true });
408
- viewerInstance = bot.viewer;
409
- logger.info("[RAM Tasarrufu] İstek geldi, 3D Görüntüleyici 3000 portunda başlatıldı.");
410
- setTimeout(() => next(), 500);
411
- return;
412
  } catch (err) {
413
  logger.error("Görüntüleyici başlatılamadı: " + err);
414
  }
@@ -421,54 +418,27 @@ const checkAndStartViewer = (req, res, next) => {
421
  next();
422
  };
423
 
424
- // HTTP İstek Yönlendirici (Proxy olmadan, ham Node.js ile)
425
- app.use('/3dview', checkAndStartViewer, (req, res) => {
426
- const targetUrl = `http://localhost:3000${req.url}`;
427
-
428
- const proxyReq = http.request(targetUrl, {
429
- method: req.method,
430
- headers: req.headers
431
- }, (proxyRes) => {
432
- res.writeHead(proxyRes.statusCode, proxyRes.headers);
433
- proxyRes.pipe(res, { end: true });
434
- });
435
-
436
- proxyReq.on('error', (err) => {
437
- logger.error("[Bağlantı Hatası] Veri aktarılamadı: " + err.message);
438
- if (!res.headersSent) res.status(502).send("Görüntü yükleniyor, lütfen sayfayı yenileyin.");
439
- });
440
-
441
- req.pipe(proxyReq, { end: true });
442
  });
443
 
444
- // Hugging Face Ana Sunucusu
445
  const server = app.listen(process.env.PORT || 7860, () => {
446
- logger.info("Ana Express sunucusu 7860 portunda aktif.");
447
  });
448
 
449
- // Ham TCP Soket Tüneli (Websocket el sıkışmalarını doğrudan 3000 portuna bağlar)
 
450
  server.on('upgrade', (req, socket, head) => {
451
- if (req.url.includes('socket.io') || req.url.startsWith('/3dview')) {
452
- const targetUrl = req.url.replace('/3dview', '');
453
-
454
- const proxyReq = http.request({
455
- host: 'localhost',
456
- port: 3000,
457
- path: targetUrl,
458
- method: req.method,
459
- headers: req.headers
460
- });
461
-
462
- proxyReq.on('upgrade', (proxyRes, proxySocket, proxyHead) => {
463
- socket.write('HTTP/1.1 101 Switching Protocols\r\n' +
464
- Object.keys(proxyRes.headers).map(key => `${key}: ${proxyRes.headers[key]}`).join('\r\n') +
465
- '\r\n\r\n');
466
-
467
- proxySocket.pipe(socket);
468
- socket.pipe(proxySocket);
469
- });
470
-
471
- proxyReq.end();
472
  }
473
  });
474
 
 
393
 
394
  app.get("/", (req, res) => res.send("Bot Aktif"));
395
 
 
396
  const checkAndStartViewer = (req, res, next) => {
397
  if (viewerTimeout) {
398
  clearTimeout(viewerTimeout);
 
403
 
404
  if (!viewerInstance && isBotReady) {
405
  try {
406
+ mineflayerViewer(bot, { firstPerson: true, prefix: '/3dview' });
407
+ viewerInstance = bot.viewer;
408
+ logger.info("[Viewer] 3D Harita motoru Express içine başarıyla enjekte edildi.");
 
 
409
  } catch (err) {
410
  logger.error("Görüntüleyici başlatılamadı: " + err);
411
  }
 
418
  next();
419
  };
420
 
421
+ // 3. KRİTİK NOKTA: Proxy'yi kaldırıp kütüphanenin kendi router'ını Express'e bağlıyoruz
422
+ app.use('/3dview', checkAndStartViewer, (req, res, next) => {
423
+ // Prismarine-viewer arka planda bir express router barındırır.
424
+ // Bu router'ı doğrudan bizim ana uygulamamıza bağlıyoruz.
425
+ if (bot.viewer && bot.viewer.app) {
426
+ return bot.viewer.app(req, res, next);
427
+ }
428
+ res.status(502).send("Harita motoru yükleniyor, lütfen sayfayı yenileyin.");
 
 
 
 
 
 
 
 
 
 
429
  });
430
 
431
+ // 4. Tek Çıkış Kapısı (Hugging Face Ana Sunucusu)
432
  const server = app.listen(process.env.PORT || 7860, () => {
433
+ logger.info("Ana Express sunucusu 7860 portunda aktif. Tüm rotalar tek kapıdan çıkıyor!");
434
  });
435
 
436
+ // 5. WEBSOCKET EL SIKIŞMA ENJEKSİYONU
437
+ // Alt dizinden gelen websocket paketlerini aracı olmadan doğrudan kütüphanenin kendi entegre socket yapısına paslıyoruz
438
  server.on('upgrade', (req, socket, head) => {
439
+ if (bot.viewer && bot.viewer.ws && (req.url.includes('socket.io') || req.url.startsWith('/3dview'))) {
440
+ // Hugging face üzerindeki ham websocket'i doğrudan harita motorunun kalbine mühürlüyoruz
441
+ bot.viewer.ws.server.emit('connection', socket, req);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
  }
443
  });
444