-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (99 loc) · 3.21 KB
/
Copy pathserver.js
File metadata and controls
109 lines (99 loc) · 3.21 KB
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
var static = require('node-static');
var express = require('express');
var applet = express();
// the pem module creates an ssl certificate and key that let you serve
// content over https ( which is necessary for web rtc)
var pem = require('pem');
// secure http protocol
var https = require('https');
var requestify = require('requestify');
applet.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
console.log('server running');
// applet.use(express.static('public'))
applet.use(express.static(__dirname + '/public'));
// create a route for the arduino
applet.get('/shirt', function(req, res){
// console.log('hit ON route');
requestify.get('http://172.22.6.60/SHIRT').then(function(response) {
// console.log('sending to arduino');
var data = response.getBody();
});
res.send('shirt');
});
applet.get('/pants', function(req, res){
// console.log('hit OFF route');
// make a request to the arduino url
requestify.get('http://172.22.6.60/PANTS').then(function(response) {
// console.log('sending to arduino');
var data = response.getBody();
});
res.send('pants');
});
applet.get('/socks', function(req, res){
// console.log('hit OFF route');
// make a request to the arduino url
requestify.get('http://172.22.6.60/SOCKS').then(function(response) {
// console.log('sending to arduino');
var data = response.getBody();
});
res.send('socks');
});
applet.get('/shoes', function(req, res){
// console.log('hit OFF route');
// make a request to the arduino url
requestify.get('http://172.22.6.60/SHOES').then(function(response) {
// console.log('sending to arduino');
var data = response.getBody();
});
res.send('shoes');
});
var clients = 0;
// you're creating a static server because you're just serving a single static
// html file
var file = new(static.Server)();
// the 'createCertificate function takes two arguments:
// 1: the options for the certificate
// 2: a callback function that creates the keys, you create your server
// inside this callback function
pem.createCertificate({days:1, selfSigned:true}, function(err,keys){
// create a server, passing in an object taht contains your newly
// generated key and certificate, then server the html file and listen
// on port 3000
var app = https.createServer({
key: keys.serviceKey,
cert: keys.certificate,
}
,applet)
.listen(3003);
const options = {
key: keys.serviceKey,
cert: keys.certificate
};
// var app = https.createServer(options, applet).listen(3000);
var io = require('socket.io')(app);
io.on('connection', function(socket){
console.log('client connected', socket.id);
clients++;
io.sockets.emit('broadcast',{ description: clients + ' clients connected!'});
socket.on("shirt",function(){
socket.broadcast.emit("shirt");
});
socket.on("pants",function(){
socket.broadcast.emit("pants");
});
socket.on("socks",function(){
socket.broadcast.emit("socks");
});
socket.on("shoes",function(){
socket.broadcast.emit("shoes");
});
socket.on('disconnect', function () {
clients--;
io.sockets.emit('broadcast',{ description: clients + ' clients connected!'});
});
});
});