-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
31 lines (25 loc) · 821 Bytes
/
server.js
File metadata and controls
31 lines (25 loc) · 821 Bytes
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
const path = require('path');
const express = require('express');
const http = require("http");
const socketio = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.get('/', (req, res) => {
res.send('Express is up and running!');
})
const port = process.env.PORT || 3000;
const publicDirectoryPath = path.join(__dirname, '/public');
app.use(express.static(publicDirectoryPath));
io.on("connection",(client)=>{
console.log('New websocket connection');
client.on('messageFromClient', msg => {
io.emit('messageFromServer', msg);
});
client.on('disconnect', () => {
console.log('New websocket disconnected');
});
})
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});