-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
55 lines (44 loc) · 1.58 KB
/
server.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var path = require('path')
var express = require('express')
// Set up Express web framework and HTTP web server
var app = express()
var http = require('http').Server(app)
// Allow our 'public' directory to be viewable
app.use(express.static(path.join(__dirname, './public')))
// Set up test route
app.route('/hello')
.get(function(req, res) {
// Simple "Hello, world!"
res.json('Hello, world!')
})
////////////////////////////////// Socket.IO ///////////////////////////////////
// TODO:
// [ ] Broadcast a message to connected users when someone connects or disconnects
// [ ] Add a MOTD (Message of the Day)
// [ ] Allow clients to choose a nickname
// [ ] Add “{user} is typing” functionality
// [ ] Show number of clients connected
// [ ] Add private messaging
// [ ] Add rate-limiting
// [ ] Admin dashboard and tools (view logs, statistics, ip ban, etc.)
// Set up socket.io
var io = require('socket.io')(http)
// This function is run every time a new client connects
// It binds listeners to events coming from that client
io.on('connection', function(client) {
console.log('%s connected :)', client.id)
// main message event
client.on('message', function(msg) {
console.log('%s sent a message: %s', client.id, msg)
io.emit('message', msg) // forward message to all connected users
})
client.on('disconnect', function() {
console.log('%s disconnected :(', client.id)
})
})
////////////////////////////////////////////////////////////////////////////////
// Start your engines!
var PORT = process.env.PORT || 5000
http.listen(PORT, function() {
console.log('Listening on %s', PORT)
})