-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (105 loc) · 3.3 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var express = require('express'),
http = require('http'),
URL = require('url'),
Github = require('github-api'),
app = express(),
server = require('http').createServer(app),
mongoose = require('mongoose');
/* Get envorinment variables for use with cloud server (e.g Heroku)
or local port variables if application hosted locally
*/
var port = process.env.PORT||3700,
mongoUri = process.env.MONGOLAB_URI || 'mongodb://localhost';
// Connect to MongoDB
mongoose.connect (mongoUri, function(err){
if(err){
console.log(err);
}
else {
console.log('Connected to Mongo');
}
});
// Create a schema to store data with Mongoose
var chatSchema = mongoose.Schema({
username: String,
message: String,
created: {type: Date, default: Date.now}
});
var chat = mongoose.model('messages', chatSchema);
app.get("/", function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.use(express.static(__dirname + '/public')); // Connect to front-end logic
var io = require('socket.io').listen(server);
// Instantiate Github api
var github = new Github({
apiUrl: "https://api.github.com"
});
// Execute upon connection with a client
io.sockets.on('connection', function (socket) {
//Load 10 previous messages in history by most recent
//chatHead tracks of the top/head of the history shown
var chatHead = 0;
var loadten = 10;
// Retrieve chat logs
chat.find(function(err, docs){
if(err) console.log(err);
else {
chatHead = docs.length;
}
});
//Shift chatHead and chatTail to print 10 older messages
socket.on('load', function (data) {
chat.find(function(err, docs){
if(err) console.log(err);
else {
if (chatHead > 0){
var chatTail = chatHead;
chatHead = chatHead - loadten;
if (chatHead < 0 )
chatHead = 0;
for (var i = chatTail - 1; i >= chatHead; i--){
// Pass each chat log's username and message one at a time to
var logdata = {username: docs[i].username, message: docs[i].message}
socket.emit('oldmessage', logdata);
}
}
else {
var logdata = {username: "Server", message: ""} //Tell client: no more messages
socket.emit('oldmessage', logdata);
}
}
});
});
// Receive message and username from client
socket.on('send', function (data) {
var msg = new chat(data);
msg.save(function(err){ // Save our message in our "chat" collection
if(err){
console.log(err);
}
else {
io.sockets.emit('newmessage', data); // Pass the message to client side
}
});
});
//Show first 5 results from "/github" <arg> command or total_count size if less than 5
var maxresult = 5;
socket.on('searchgithub', function (data) {
var command = "/github " + data;
var result = "";
socket.emit('commandresult',{command:command, result: result});
// Search for <arg> in repo name, description, and readme. Sort by most recently updated.
var search = github.getSearch(data + "+in%3Aname%2Cdescription%2Creadme&sort=updated&order=desc");
search.repositories(null, function (err, repositories) {
if (repositories.total_count < maxresult)
maxresult = repositories.total_count;
for (var i = 0; i < maxresult; i++){
result = repositories.items[i];
socket.emit('commandresult',{command:command, result: result});
}
});
});
});
server.listen(port);
console.log("Listening on port " + port);