-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (28 loc) · 1.14 KB
/
Copy pathserver.js
File metadata and controls
35 lines (28 loc) · 1.14 KB
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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const moment = require('moment');
const logFormat = 'MMM D, YYYY HH:mm:ss';
const ch = require('chalk');
/* Serve up dummy html page for testing */
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
/* Handle socket connections */
io.on('connection', (socket) => {
console.log(`User '${socket.id}' connected`);
socket.on('msg-main', (msg) => {
console.log(`${ch.red.bgBlack(moment().format(logFormat))}: User ${ch.blue(socket.id)} sent msg: ${ch.yellow(msg)}`);
socket.broadcast.emit('msg-main', msg, socket.id);
});
socket.on('msg-private', (msg, targetUser) => {
console.log(`${ch.red.bgBlack(moment().format(logFormat))}: User ${ch.blue(socket.id)} sent pm to user ${ch.blue(targetUser)}: ${ch.yellow(msg)}`);
io.to(targetUser).emit('msg-private', msg, socket.id);
});
});
/* SPin up server */
var port = process.env.PORT || 3001;
http.listen(port, () => {
console.log(`Chat server listening at http://localhost:${port}`);
});