
import { useState, useEffect } from "react";

export default function DemoLink() {
  const [price, setPrice] = useState("");
  const [date, setDate] = useState("");

  const subscription = { "Firm": "DEMO", "PrivateKey": "A@1122", "ApiKey": "CRIC@20" };
 
  
  useEffect(() => {
    const ws = new WebSocket("ws://148.251.21.118:5570");

    ws.onopen = () => {
      console.log("Connection Established!");
      //ws.send(JSON.stringify(subscription));
    };
    ws.onmessage = (event) => {
      const response = JSON.parse(event.data);
      //if(response.responseType==11){
        console.log(response)
      //}
      //ws.close();
    };
    ws.onclose = () => {
      console.log("Connection Closed!");
      //initWebsocket();
    };

    ws.onerror = () => {
      console.log("WS Error");
    };

    return () => {
      ws.close();
    };
  }, []);

  // useEffect(() => {
  //   setTimeout(initWebsocket(), 10000);
  // }, []);

  return (
    <div>
      <p>Price : {price}</p>
      <p>Last Update: {date}</p>
    </div>
  );
}
