How to send socket data from Django website?

Viewed 33

I have a simple website with a button on it and I would like to connect that button to a websocket and send arbitrary data to the socket once it is pressed. I need to calculate the time it takes for the signal to be sent and received once the button is pressed.

html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Increment</title>
</head>
<body>
{% block content %}
Increment Counter
<form id="form" method="post" action="#">
    {% csrf_token %}
    <button type="submit", name="increment", value="increment">Increment</button>
</form>

<script type="text/javascript">
    let url = `ws://${window.location.host}/ws/socket-server/`

    const chatSocket = new WebSocket(url)

    chatSocket.onmessage = function(e){
        let data = JSON.parse(e.data)
        console.log('Data:', data)
    }

</script>
{% endblock %}

</body>
</html>

I am very inexperienced so I am unsure how to do this.

0 Answers
Related