Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var connection;
- var endpoint = '192.168.1.99';
- var port = '1200';
- function onLoad() {
- if (checkWSSupport()) {
- var connection = new WebSocket(endpoint, port);
- }
- }
- function checkWSSupport() {
- if ('WebSocket' in window) {
- console.log("WebSockets are supported in this browser.");
- return true;
- } else {
- console.log("WebSockets are NOT supported in this browser.");
- return false;
- }
- }
- function initWebSocket(ep, p) {
- console.log("Attempting to initialize WebSocket...");
- try {
- connection = new WebSocket('ws://' + ep + ':' + p);
- } catch (error) {
- console.log("Could not initialize WebSocket...");
- }
- connection.onopen = function() { // Runs when WebSocket connection is successfully opened.
- console.log("WebSocket connection has opened...");
- };
- connection.onmessage = function(e) { // Runs when the WebSocket connection receives a frame.
- console.log("RECEIVED: " + e.data);
- }
- connection.onerror = function(error) { // Runs when the WebSocket connection errors.
- console.log("WebSocket Error: " + error.data);
- }
- connection.onclose = function() { // Runs when the WebSocket connection is closed.
- console.log("WebSocket connection has closed.");
- }
- }
- function reloadWebSocket() {
- console.log("Reloading WebSocket...");
- connection = null;
- initWebSocket(endpoint, port);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement