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

WebSocket Provider HandShake Error #2076

Closed
InfernalHeir opened this issue Sep 22, 2021 · 1 comment
Closed

WebSocket Provider HandShake Error #2076

InfernalHeir opened this issue Sep 22, 2021 · 1 comment

Comments

@InfernalHeir
Copy link

InfernalHeir commented Sep 22, 2021

Describe the bug

i am trying to indexing the blockchain data by web sockets providers but unfortunately got error.

javascript

/Users/bhupendrabisht/u_XX-indexing-all/node_modules/ws/lib/websocket.js:604
     abortHandshake(
     ^
Error: Unexpected server response: 200
   at ClientRequest.<anonymous> (/Users/bhupendrabisht/u_XX-indexing-all/node_modules/ws/lib/websocket.js:604:7)
   at ClientRequest.emit (events.js:400:28)
   at ClientRequest.emit (domain.js:470:12)
   at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:647:27)
   at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
   at TLSSocket.socketOnData (_http_client.js:515:22)
   at TLSSocket.emit (events.js:400:28)
   at TLSSocket.emit (domain.js:470:12)
   at addChunk (internal/streams/readable.js:290:12)
   at readableAddChunk (internal/streams/readable.js:265:9)
[nodemon] app crashed - waiting for file changes before starting...

Code Snippet 

import { logger } from "../../log";
import { wsEthProvider } from "../../providers/provider";

import { EXPECTED_PONG_BACK, KEEP_ALIVE_CHECK_INTERVAL } from "../events";

export const startConnection = () => {
  let pingTimeout: any = null;
  let keepAliveInterval: any = null;

  wsEthProvider._websocket.on("open", () => {
     keepAliveInterval = setInterval(() => {
        logger.debug("Checking if the connection is alive, sending a ping");

        wsEthProvider._websocket.ping();
        // Use `WebSocket#terminate()`, which immediately destroys the connection,
        // instead of `WebSocket#close()`, which waits for the close timer.
        // Delay should be equal to the interval at which your server
        // sends out pings plus a conservative assumption of the latency.
        pingTimeout = setTimeout(() => {
           wsEthProvider._websocket.terminate();
        }, EXPECTED_PONG_BACK);
     }, KEEP_ALIVE_CHECK_INTERVAL);

     // TODO: handle contract listeners setup + indexing
     wsEthProvider.on("block", (blockNumber) => {
        logger.info(`latest block ${blockNumber}`);
     });
  });

  wsEthProvider._websocket.on("close", () => {
     logger.error("The websocket connection was closed");
     clearInterval(keepAliveInterval);
     clearTimeout(pingTimeout);
     startConnection();
  });

  wsEthProvider._websocket.on("pong", () => {
     logger.debug(
        "Received pong, so connection is alive, clearing the timeout"
     );
     clearInterval(pingTimeout);
  });

  wsEthProvider._websocket.on("error", () => {
     logger.error(`WSS:: Connection error we restart the server again`);
     startConnection();
  });
};
startConnection();
"../../providers/provider.ts"

export const wsEthereumCohorts = (cohortAddress: string) => {
   return ethSwitch(cohortAddress, wsEthProvider);
};

"../../providers/switch.ts"

import { JsonRpcProvider, WebSocketProvider } from "@ethersproject/providers";
import { Contract } from "ethers";
import { V1, V2, V3, V4 } from "../constants";
import UN**ABI from "../constants/ethereum/UN_xx.json";
import EthereumCohortABI from "../constants/ethereum/ABI.json";

export const ethSwitch = (
   address: string,
   provider: JsonRpcProvider | WebSocketProvider
) => {
   if (address.toLowerCase() === V1.toLowerCase()) {
      return new Contract(address, UN**ABI, provider);
   } else if (address.toLowerCase() === V2.toLowerCase()) {
      return new Contract(address, UN**ABI, provider);
   } else if (address.toLowerCase() === V3.toLowerCase()) {
      return new Contract(address, UN**ABI, provider);
   } else if (address.toLowerCase() === V4.toLowerCase()) {
      return new Contract(address, UN**ABI, provider);
   }
   return new Contract(address, EthereumCohortABI, provider);
};

Environment
Node - v14.17.5
@ethersproject/providers: ^5.4.5
Desktop OS - MacOS Big Sur

i have found #1053

@InfernalHeir InfernalHeir added the investigate Under investigation and may be a bug. label Sep 22, 2021
@InfernalHeir
Copy link
Author

Wrong provider given thats why.

@ricmoo ricmoo removed the investigate Under investigation and may be a bug. label Oct 20, 2021
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

2 participants