Skip to content

WebSocket Provider HandShake Error #2076

@InfernalHeir

Description

@InfernalHeir

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions