-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (28 loc) · 893 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// server.js
// Require and create our server packages
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
// Send socket initialization scripts to the client
app.get('/', function(req, res){
res.send(`
<script src="/socket.io/socket.io.js"></script>
<script>
let socket = io();
socket.on('text', (txt) => {
let textp = document.createElement("h1");
let t = document.createTextNode(txt);
textp.appendChild(t);
document.body.appendChild(textp);
});
</script>`);
});
// Respond to socket connections with a Hello World text
io.on('connection', (socket) => {
console.log('User connected');
io.emit('text', 'Hello, World!');
});
// Run our socket-enabled server
http.listen(7777, function() {
console.log('listening on *:7777');
});