Created
November 16, 2018 14:39
-
-
Save Srushtika/be1ad08c3142c1015e4942e0233583b8 to your computer and use it in GitHub Desktop.
WebSockets server tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Establish a WebSocket connection to the echo server | |
const ws = new WebSocket('wss://echo.websocket.org'); | |
// Add a listener that will be triggered when the WebSocket is ready to use | |
ws.addEventListener('open', () => { | |
const message = 'Hello!'; | |
console.log('Sending:', message); | |
// Send the message to the WebSocket server | |
ws.send(message); | |
}); | |
// Add a listener in order to process WebSocket messages received from the server | |
ws.addEventListener('message', event => { | |
// The `event` object is a typical DOM event object, and the message data sent | |
// by the server is stored in the `data` property | |
console.log('Received:', event.data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment