I was having the same issue. The error was that I was running my HTML code with Live Server extension in VSCode. When the file is created, the entire folder is updated, and Live Server reloads the page, restarting the WebSocket connection. Saving the file in a folder outside the opened VSCode folder or opening the plain HTML file solved my problem.