-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
71 lines (58 loc) · 2.12 KB
/
web.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
70
71
var fs = require('fs'),
http = require('http'),
nowjs = require('now');
var server = http.createServer(function(req, response)
{
var route = {
/* ROUTE PAGES */
'/': ['/html/view.html', 'text/html'],
'/client': ['/html/client.html', 'text/html'],
/* SCRIPTS */
'/js/jquery.js': ['/js/jquery.js', 'text/javascript'],
'/js/underscore.js': ['/js/underscore.js', 'text/javascript'],
/* STYLES */
'/css/screen.css': ['/css/screen.css', 'text/css'],
/* IMAGES */
'/img/glove-apple-touch-icon-precomposed.png':
['/img/glove-apple-touch-icon-precomposed.png', 'image/png'],
}[req.url];
if (route != undefined){
var type = (route[1] || 'text/plain');
fs.readFile(__dirname+route[0], function(err, data){
response.writeHead(200, {'Content-Type':type});
response.write(data);
response.end();
});
}
else{
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end("Page Could Not Be Found");
}
});
server.listen(8080 || process.env.PORT );
console.log("Server is listening on " + 8080 || process.env.PORT );
var nowjs = require("now");
var everyone = nowjs.initialize(server);
nowjs.on('connect', function(){
this.now.room = "room 1";
nowjs.getGroup(this.now.room).addUser(this.user.clientId);
console.log("Joined: " + this.now.name);
});
nowjs.on('disconnect', function(){
console.log("Left: " + this.now.name);
});
everyone.now.changeRoom = function(newRoom){
this.now.distributeMessage("[leaving " + this.now.room + "]");
nowjs.getGroup(this.now.room).removeUser(this.user.clientId);
nowjs.getGroup(newRoom).addUser(this.user.clientId);
this.now.room = newRoom;
this.now.distributeMessage("[entering " + this.now.room + "]");
var that = this;
nowjs.getGroup(this.now.room).count(function(count){
var prettyCount = (count === 1) ? "Room is empty." : (count - 1) + " other(s) in room.";
that.now.receiveMessage("SERVER", "You're now in " + that.now.room + ". " + prettyCount);
});
}
everyone.now.distributeMessage = function(message){
nowjs.getGroup(this.now.room).now.receiveMessage(this.now.name, message);
};