Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client hero.close() bug on hero core server #352

Open
leonardo3dp opened this issue Jan 22, 2025 · 0 comments
Open

client hero.close() bug on hero core server #352

leonardo3dp opened this issue Jan 22, 2025 · 0 comments

Comments

@leonardo3dp
Copy link

leonardo3dp commented Jan 22, 2025

I created a client and a server and made a http server for the server. and I use const hero = new Hero({ userAgent: '~ chrome >= 112 && windows >= 10', name:'teste1', // I need it to be the same name showChrome: true, noChromeSandbox: true, disableIncognito:true, connectionToCore: { host: 'localhost:3000/hero' }, }); to connect to the server

and when using await hero.close(); gera um erro no servidor

ERROR [hero-core\index] UnhandledRejection { context: {}, sessionId: null, sessionName: undefined } Error: WebSocket is not open: readyState 2 (CLOSING) at sendAfterClose (D:\nodejs\navegador_scraping\node_modules\ws\lib\websocket.js:1134:17) at WebSocket.send (D:\nodejs\navegador_scraping\node_modules\ws\lib\websocket.js:461:7) at D:\nodejs\navegador_scraping\node_modules\net\lib\WsUtils.ts:14:8 at new Promise (<anonymous>) at wsSend (D:\nodejs\navegador_scraping\node_modules\net\lib\WsUtils.ts:13:9) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async WsTransportToClient.send (D:\nodejs\navegador_scraping\node_modules\net\lib\WsTransportToClient.ts:40:7) at async ConnectionToHeroClient.handleRequest (D:\nodejs\navegador_scraping\node_modules\core\connections\ConnectionToHeroClient.ts:104:7)

and when trying to use the client again the page loads with an error

HTTP/1.1 407 Proxy Authentication Required Proxy-Authenticate: Basic realm="agent"

My server code

`import { CorePlugin } from '@ulixee/hero-plugin-utils';
import HeroCore from '@ulixee/hero-core';
import ExecuteJsPlugin from '@ulixee/execute-js-plugin';

import { WsTransportToClient } from '@ulixee/net';
import { WebSocketServer } from 'ws';
import * as http from 'http';

const heroCore = new HeroCore();

HeroCore.use(ExecuteJsPlugin);

heroCore.on('error', (error) => {
console.error('Erro no Hero Core:', error);
});

heroCore.on('close', () => {
console.log('Hero Core encerrado');
});

// Attach Hero to your Http or Https Server
async function bindHeroCore(yourHttpServer) {

const wsServer = new WebSocketServer({
server: yourHttpServer,
});

wsServer.on('connection', (ws, req) => {
// OPTIONAl: it's configured to listen on a path
if (req.url && req.url.startsWith('/hero')) {
const transport = new WsTransportToClient(ws, req);
const connection = heroCore.addConnection(transport);
//console.log(connection.core)

  ws.on('close', (code, reason) => {
	//connection.disconnect()
    console.log('WebSocket encerrado');
  });
  
  ws.on('error', (err) => {
	 //connection.disconnect()
    console.error('Erro no WebSocket:', err);
  });
  
  
}
else {
  ws.close(4001, 'Rota inválida'); // Fecha a conexão se não for para /hero
}

});

console.log('Hero Core vinculado ao servidor WebSocket');
}

// Criação de um servidor HTTP simples
const httpServer = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Servidor Hero Core está rodando. Use WebSocket em /hero');
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Rota não encontrada');
}
});

// Vincula o Hero Core ao servidor e inicia
bindHeroCore(httpServer).then(() => {
const PORT = 3000;
httpServer.listen(PORT, () => {
console.log('Servidor rodando em http://localhost:'+PORT);
console.log('Use a rota WebSocket em ws://localhost:3000/hero');
});
}).catch(err => {
console.error('Erro ao vincular Hero Core:', err);
});`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant