Skip to content

Instantly share code, notes, and snippets.

@huangsam
Created January 18, 2018 14:34
Show Gist options
  • Save huangsam/21610a6af4f5bc0f05cd6fbfd2fa2637 to your computer and use it in GitHub Desktop.
Save huangsam/21610a6af4f5bc0f05cd6fbfd2fa2637 to your computer and use it in GitHub Desktop.
Testing WebSockets locally and remotely
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"async-limiter": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
},
"safe-buffer": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
"integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
},
"ultron": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz",
"integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og=="
},
"ws": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-4.0.0.tgz",
"integrity": "sha512-QYslsH44bH8O7/W2815u5DpnCpXWpEK44FmaHffNwgJI4JMaSZONgPBTOfrxJ29mXKbXak+LsJ2uAkDTYq2ptQ==",
"requires": {
"async-limiter": "1.0.0",
"safe-buffer": "5.1.1",
"ultron": "1.1.1"
}
}
}
}
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:8080"
var output
function init() {
output = document.getElementById("output")
testWebSocket()
}
function testWebSocket() {
websocket = new WebSocket(wsUri)
websocket.onopen = function(evt) {
onOpen(evt)
}
websocket.onmessage = function(evt) {
onMessage(evt)
}
websocket.onerror = function(evt) {
onError(evt)
}
websocket.onclose = function(evt) {
onClose(evt)
}
}
function onOpen(evt) {
writeToScreen("CONNECTED")
setTimeout(function () {
doSend("WebSocket rocks")
doSend("Java sucks")
}, 500)
setTimeout(function () {
websocket.close()
}, 1000)
}
function onMessage(evt) {
writeToScreen('<span style="color: blue">RESPONSE: ' + evt.data + '</span>')
}
function onError(evt) {
writeToScreen('<span style="color: red">ERROR:</span> ' + evt.data)
}
function onClose(evt) {
writeToScreen('DISCONNECTED')
}
function doSend(message) {
writeToScreen("SENT: " + message)
websocket.send(message)
}
function writeToScreen(message) {
var pre = document.createElement("p")
pre.style.wordWrap = "break-word"
pre.innerHTML = message
output.appendChild(pre)
}
window.addEventListener("load", init, false)
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>
</html>
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://echo.websocket.org/"
var output
function init() {
output = document.getElementById("output")
testWebSocket()
}
function testWebSocket() {
websocket = new WebSocket(wsUri)
websocket.onopen = function(evt) {
onOpen(evt)
}
websocket.onmessage = function(evt) {
onMessage(evt)
}
websocket.onerror = function(evt) {
onError(evt)
}
websocket.onclose = function(evt) {
onClose(evt)
}
}
function onOpen(evt) {
writeToScreen("CONNECTED")
setTimeout(function () {
doSend("WebSocket rocks")
doSend("Java sucks")
}, 500)
setTimeout(function () {
websocket.close()
}, 1000)
}
function onMessage(evt) {
writeToScreen('<span style="color: blue">RESPONSE: ' + evt.data + '</span>')
}
function onError(evt) {
writeToScreen('<span style="color: red">ERROR:</span> ' + evt.data)
}
function onClose(evt) {
writeToScreen('DISCONNECTED')
}
function doSend(message) {
writeToScreen("SENT: " + message)
websocket.send(message)
}
function writeToScreen(message) {
var pre = document.createElement("p")
pre.style.wordWrap = "break-word"
pre.innerHTML = message
output.appendChild(pre)
}
window.addEventListener("load", init, false)
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>
</html>
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send(message);
});
ws.send('We are in business');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment