-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
65 lines (56 loc) · 1.84 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
// Serve Static Files
var express = require('express');
var app = express();
var oneDay = 86400000;
app.use(express.static(__dirname + '/public_files', { maxAge: oneDay}));
app.listen(process.env.PORT || 1313);
// Primus
'use strict';
var Primus = require('primus')
, http = require('http')
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.end("Primus!?!?!?");
}), primus = new Primus(server,{});
primus.save(__dirname + '/public_files/js/primus.js');
var ipToSparks = {};
primus.on('connection', function (spark) {
var ip = spark.address.ip + "" + new Date().getTime();
ipToSparks[ip] = spark;
spark.write({'ip':ip});
spark.on('data',function message(data) {
if (data.phoneNumber) {
sendText(data);
return;
}
if (data.ip) {
spark.browserSpark = ipToSparks[data.ip + ""];
console.log("Paired: " + spark.address.ip + " to " + data.ip);
} else {
if (spark.browserSpark) {
spark.browserSpark.write(data);
} else {
console.log("no browser spark?");
}
}
});
console.log("Connect: " + spark.address.ip);
});
primus.on('disconnection', function(spark) {
console.log("Disconnect: " + spark.address.ip);
});
function sendText(data) {
// Twilio Credentials
var accountSid = 'ACe721241e621a0c3a6b05cc0544be00e6';
var authToken = '1786fba688cf80ed8bd6fe47d0ab9f4f';
//require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken);
client.messages.create({
to: data.phoneNumber,
from: "+14104151371",
body: data.url,
}, function(err, message) {
console.log(err);
});
}
server.listen(1310);