Skip to content

Instantly share code, notes, and snippets.

@wayneseymour
Created October 7, 2020 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wayneseymour/1a5146093aedc8d09881c939393eaf4d to your computer and use it in GitHub Desktop.
Save wayneseymour/1a5146093aedc8d09881c939393eaf4d to your computer and use it in GitHub Desktop.
SImple echo spike
// Start the connection to the WebSocket server at echo.websocket.org
ws = new WebSocket("ws://echo.websocket.org/");
// Register event listeners for the open, close, and message events
ws.onopen = () => {
console.log("WebSocket ready!");
console.log('yo')
ws.send('yo')
};
ws.onmessage = (message) => {
// Log the message we recieve:
console.log("Received data:", message.data);
// Close the websocket after receiving the "exit" message (the last line)
if(message.data == "exit") ws.close();
};
ws.onclose = () => console.log("WebSocket closed!");
ws.onerror = (err) => console.log("WebSocket error:", err);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment