Last active
June 12, 2022 15:37
-
-
Save KMurphs/6efcbfe36b2d680f6fd3741796e846b9 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 SimpleWebSocket(address) { | |
if(!("WebSocket" in window)) { | |
console.log("WebSocket NOT supported by your Browser!"); | |
return null | |
} | |
// Let us open a web socket | |
var ws = new WebSocket(address); | |
ws.onopen = function() { | |
// Web Socket is connected, send data using send() | |
console.log("Socket is opened..."); | |
}; | |
ws.onmessage = function (evt) { | |
var received_msg = evt.data; | |
console.log("Message is received...", received_msg); | |
}; | |
ws.onclose = function() { | |
// websocket is closed. | |
console.log("Connection is closed..."); | |
}; | |
console.log("WebSocket is supported by your Browser!"); | |
return ws; | |
} | |
const address = "ws://127.0.0.1:8010"; | |
var ws = SimpleWebSocket(address); | |
ws?.send("My awesome message"); | |
ws?.send("My second awesome message"); |
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
var msg = "My awesome message"; | |
const address = "ws://127.0.0.1:8010"; | |
(function WebSocketTest() { | |
if ("WebSocket" in window) { | |
console.log("WebSocket is supported by your Browser!"); | |
// Let us open a web socket | |
var ws = new WebSocket(address); | |
ws.onopen = function() { | |
// Web Socket is connected, send data using send() | |
ws.send(msg); | |
console.log("Message is sent..."); | |
}; | |
ws.onmessage = function (evt) { | |
var received_msg = evt.data; | |
console.log("Message is received...", received_msg); | |
}; | |
ws.onclose = function() { | |
// websocket is closed. | |
console.log("Connection is closed..."); | |
}; | |
} else { | |
// The browser doesn't support WebSocket | |
console.log("WebSocket NOT supported by your Browser!"); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment