Created
June 19, 2018 19:39
-
-
Save porsager/fc129c9917a0b3d81b5cab13676dbcf5 to your computer and use it in GitHub Desktop.
This file contains 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
<!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> |
This file contains 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
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