Why did I get the "Chat socket closed unexpectedly" exception?

Asked

Viewed 23 times

0

I received the exception "Chat socket closed unexpectedly" in this javascript script:

const chatSocket = new WebSocket(
            'ws://'
            + window.location.host
            + '/ws/chat/'
            + roomName
            + '/'
        );

        chatSocket.onmessage = function(e) {
            const data = JSON.parse(e.data);
            document.querySelector('#chat-log').value += (data.message + '\n');
        };

        chatSocket.onclose = function(e) {
            console.error('Chat socket closed unexpectedly');
        };

What’s the problem here ?? I can’t solve it

  • The answer to your question is in itself, chatSocket is closing, so you are getting the error, since it was the output that you put to print. Try to print the event itself, and use the browser’s Debugger to help you understand the code itself :D

  • as it implemeted the chatSocket.onclose, implement the chatSocket.onopen to see if you connected correctly and see if there are errors

  • Thank you I’ll do it

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.