Skip to content

Instantly share code, notes, and snippets.

@porsager
Created June 19, 2018 19:39
Show Gist options
  • Save porsager/fc129c9917a0b3d81b5cab13676dbcf5 to your computer and use it in GitHub Desktop.
Save porsager/fc129c9917a0b3d81b5cab13676dbcf5 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<pre id="chatter"></pre>
<script type="text/javascript" charset="utf-8">
const ws = new WebSocket('ws://localhost:3000')
ws.onmessage = ({ data }) => {
chatter.innerHTML += '\nreceived: ' + data
setTimeout(() => {
chatter.innerHTML += '\nsending : got it'
ws.send('got it')
}, 1000)
}
</script>
</body>
</html>
const http = require('http')
, ws = require('ws')
, fs = require('fs')
const indexHtml = fs.readFileSync('./index.html', 'utf8')
const server = http.createServer((req, res) => {
res.end(indexHtml, 'utf8')
})
const wss = new ws.Server({ server })
wss.on('connection', socket => {
socket.on('message', data => socket.send('You said - ' + data))
socket.send('Hi')
})
server.listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment