[Fixed]-WebSocket.onmessage doesn't work

1👍

adding the original code to the ready block is either not working?

socket.onopen = function() {
    console.log("socket open")
    socket.send("hello world");
}

It could be that the channel is not yet opened

👤thegio

0👍

That’s code that worked

    socket = new WebSocket("ws://127.0.0.1:8000/chat/");
    socket.onmessage = function (e) {
        alert(e.data);
    };
    $(document).ready(function () {
        socket.onopen = function() {
            $('button').click(function () {
                socket.send($('#mes_input').val());
            });
        }
    });

Leave a comment