-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
112 lines (84 loc) · 3.13 KB
/
server.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
const express = require('express')
const parser = require('body-parser')
const app = express()
const url = require('url')
const multer = require('multer')
const upload = multer({dest:'public/uploads/'})
const http = require('http').Server(app)
const io = require('socket.io')(http)
const fs = require('fs')
//var proxy = require('express-http-proxy');
const port = 3000
const entityserver = require('./src/entity_server.js')
//////////////////////////////////////////////////
// server
//////////////////////////////////////////////////
var request = require('request');
app.get('/proxy/*', function(req,res) {
let newurl = req.params[0]
request(newurl).pipe(res)
})
// older proxy approach - too limiting
//app.use('/github.com', proxy('github.com'))
//app.use('/raw.githubusercontent.com', proxy('raw.githubusercontent.com'))
app.use(parser.json())
app.get("/", (request, response) => {
response.sendFile(__dirname + '/public/index.html')
})
app.post('/api/entity/save', (request, response) => {
let results = entityserver.save(request.body)
response.json(results)
})
app.post('/api/entity/flush', (request, response) => {
//let results = entityserver.flush(request.body)
response.json(results)
})
app.post('/api/entity/query', (request, response) => {
let results = entityserver.query(request.body)
response.json(results)
})
app.post('/api/map/save', upload.single('blob'), (request, response) => {
let results = entityserver.map_save(request.file.path,request.body)
response.json(results)
})
var ip = require("ip");
console.dir ( "Your server http address is http://" + ip.address() + ":" + port )
app.use(express.static('public'))
// Logic for long connections
io.on('connection', (socket) => {
console.log("Server: a new connection has shown up " + socket.id)
// Sockets will tell server where they are at some point
socket.on('location', (location) => {
entityserver.socket_remember(socket.id,location)
})
// Sockets also tell server about publishing events
socket.on('publish', (msg) => {
let srcid = socket.id
// save
msg.socket_id = srcid
let results = entityserver.save(msg)
// debug
let latitude = msg.gps ? msg.gps.latitude : 0
let longitude = msg.gps ? msg.gps.longitude : 0
console.log("Server port " + srcid + " received entity=" + msg.uuid + " kind=" + msg.kind + " at lat="+latitude+" lon="+longitude )
// publish to all nearby - let's not reecho to self
let ids = Object.keys(io.sockets.sockets)
for(let i = 0; i < ids.length; i++) {
let id = ids[i]
if(srcid == id) continue
let target = io.sockets.sockets[id]
if(!entityserver.socket_nearby(srcid,id) ) {
console.log("not sending msg to geographically distant socket " + id + " " + msg.uuid)
continue
}
target.emit('publish',results)
}
})
socket.on('disconnect', (msg) => {
// TODO periodically flush boring things that are attached to dead sockets - such as ghosts of participants past
// TODO also flush participants on a dead socket now
console.log("Socket disconnect " + socket.id)
entityserver.socket_forget(socket.id)
})
})
http.listen(port)