-
Notifications
You must be signed in to change notification settings - Fork 38
/
app.js
40 lines (32 loc) · 936 Bytes
/
app.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
32
33
34
35
36
37
38
39
40
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
// set static folder
app.use(express.static(__dirname + "/public"));
// homepage
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// socket connection
io.on('connection', (socket) => {
console.log("A user connected!");
// executes when a user starts typing
socket.on('userTyping', (username) => {
socket.broadcast.emit('userTyping', username);
});
// executes when a user sends a message
socket.on('msg', (msg) => {
console.log("message: ", msg);
socket.broadcast.emit('msg', msg);
});
// executes when a new user joins the chat room
socket.on('join', (username) => {
socket.broadcast.emit('join', username);
});
});
const PORT = process.env.PORT || 5000;
// port to listen
http.listen(PORT, function(){
console.log('listening on port: ', PORT);
});