forked from nko3/codetag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
90 lines (74 loc) · 2.47 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var parser = require('url').parse;
var app = express();
var httpServer = http.createServer(app);
var io = require('socket.io').listen(httpServer);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
io.sockets.on('connection', function(socket){
socket.nickname=socket.id;
socket.on('newUrl', function(data){
var urlObj = parser(data.url);
var channel = urlObj.host+urlObj.pathname;
socket.get('channel', function(err, oldChannel){
if(oldChannel){
socket.leave(oldChannel);
}
socket.set('channel', channel, function(){
socket.join(channel);
socket.emit('message', { sender:"server", message: 'welcome to the '+channel+' channel'});
});
});
});
socket.on('message', function(msg){
var command = msg.message.trim().match('^>([a-zA-Z]+):([a-zA-Z0-9-_]+)$');
if(command){
if(command[1]=="nick"){
socket.nickname = command[2];
socket.emit('message', { sender:"server", message: 'you have changed your nickname to '+socket.nickname});
return;
}
}
socket.get('channel', function(err, channel){
console.log(channel);
socket.broadcast.to(channel).emit('message', {sender:socket.nickname, message: msg.message});
});
});
socket.on('get channels', function(){
var channels = io.sockets.manager.rooms;
socket.emit('update channels', channels);
});
socket.on('get tab users', function(info){
console.log(info);
if(info.status === 'loading'){
var urlObj = parser(info.url);
var channelUsers = io.sockets.manager.rooms[urlObj.host+urlObj.pathname];
channelUsers.tabId = info.tabId;
socket.emit('tab users', channelUsers);
console.log(channelUsers);
}
});
});
httpServer.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});