-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (57 loc) · 1.6 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var uuid = require('node-uuid');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Room = require('./room.js');
var User = require('./user.js');
var rooms = []
var defaultUserLimit = 4
function getFreeRoom() {
for (i in rooms) {
var room = rooms[i]
if(room.hasRoom()) {
return room;
}
}
var newRoom = new Room(defaultUserLimit)
rooms.push(newRoom)
return newRoom
}
app.use(express.static(__dirname + "/public"))
io.on('connection', function(socket) {
// assign a room to this user
var user = new User(socket)
// note: users won't join any room until they identify themselves
socket.on('howdy', function(username) {
user.name = username
var room = getFreeRoom()
room.addUser(user)
//display all users currently in room
})
socket.on('message', function(msg) {
// process the message here.
var messageBlob = JSON.parse(msg)
var sender = user.name
console.log(messageBlob)
switch (messageBlob.type) {
case "private_elizachat":
sender = "Amelia"
case "private":
user.socket.emit('message', {name: sender + " (Private)", text: messageBlob.message})
break;
case "elizachat":
sender = "Amelia"
default:
user.room.broadcast('message', {name: sender, text: messageBlob.message})
break;
}
})
socket.on('disconnect', function() {
user.leaveRoom()
})
})
var port = process.env.PORT || 9393
http.listen(port, function() {
console.log("Listening on *:" + port)
})