Last active
October 31, 2021 14:47
-
-
Save bharathvaj-ganesan/509dd0942f5bed492e21cfa462aa120d to your computer and use it in GitHub Desktop.
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
$(function () { | |
// if user is running mozilla then use it's built-in WebSocket | |
window.WebSocket = window.WebSocket || window.MozWebSocket; | |
const connection = new WebSocket('ws://localhost:8080/githubEvents'); | |
connection.onopen = function () { | |
// connection is opened and ready to use | |
}; | |
connection.onerror = function (error) { | |
// an error occurred when sending/receiving data | |
}; | |
connection.onmessage = function (message) { | |
// try to decode json (I assume that each message | |
// from server is json) | |
try { | |
const githubEvent = JSON.parse(message.data); // display to the user appropriately | |
} catch (e) { | |
console.log('This doesn\'t look like a valid JSON: '+ message.data); | |
return; | |
} | |
// handle incoming message | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment