import React from 'react';
import useWebSocket from 'react-use-websocket';

class Websocketclient extends React.Component
{
    ws = new WebSocket("ws://127.0.0.1:8008");
    constructor(props) {
        
        super(props);
        
        this.state = {
          val: null,
        };
      }

      sendMessage(msg )
      {
        this.ws.send(msg);

      }
      componentDidMount() {
        this.ws.onopen = () => {
          console.log("opened");
          this.ws.send("test"); // message to send on Websocket ready
        };
    
        this.ws.onclose = () => {
          console.log("closed");
        };
    
        this.ws.onmessage = (event) => {
          console.log("got message", event.data);
          this.setState({ val: event.data });
        };
      }
    
      componentWillUnmount() {
    }
    
    close()
    {    
        console.log("closing websocket...");
        this.ws.close();
    }
    
      render() {
        return <div>Value: {this.state.val}</div>;
      }
}

export default Websocketclient;