-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (82 loc) · 2.28 KB
/
index.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
var config = require("./config.json"),
utils = require("./utils.js"),
express = require("express"),
app = express(),
server = require('http').Server(app),
sio = require("socket.io")(server);
var router = require('./api.js')(express.Router(),config,sio);
app.use('/api',router);
app.use("/",express.static("public"));
tail_files = {};
default_files = []
if(config.files){
config.files.forEach(function(file){
utils.isValidFile(file,function(err){
if(err){
console.log("Default File:",file,err.message);
}
else{
hash = utils.hash(file);
default_files.push({'filename':file,'hash':hash});
utils.tail(file,hash,sio);
}
})
});
}
sio.on("connection",function(client){
console.log("socketio: new user connected: "+client.id);
client.on("disconnect",function(){
console.log("socketio: user disconnected: "+client.id);
})
client.on("message",function(data){})
/* Init from client, send all default file details*/
client.on("wt_init",function(data){
default_files.forEach(function(file){
client.join(file.hash);
client.emit("wt_new",JSON.stringify(file));
});
})
/* Register a new file */
client.on("wt_new",function(data){
data = utils.parseJSON(data,client);
if(data !== false){
hash = utils.hash(data.filename);
if(!tail_files[hash]){
utils.isValidFile(data.filename,function(err){
if(err){
client.emit("wt_error",err.message);
return
}
else{
resp = {
filename: data.filename,
hash : hash
};
client.join(hash)
client.emit("wt_new",JSON.stringify(resp));
tail_files[hash] = 1;
utils.tail(data.filename,hash,sio);
}
})
}
else{
resp = {
filename: data.filename,
hash : hash
};
client.join(hash);
client.emit("wt_new",JSON.stringify(resp));
}
}
else{
client.emit("wt_error","Invalid request");
}
});
});
server.listen(config.port||7733,function(err){
if(err){
console.log("Cannot listen on port: "+config.port||7733);
process.exit(-1)
}
console.log("Server listening on port:"+config.port||7733)
})