-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (45 loc) · 1.44 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
const express = require('express')
const compression = require('compression')
const http = require('http')
const app = express()
const server = http.createServer(app)
const PORT = process.env.PORT || 3000
app.use(compression())
app.use('/', express.static(__dirname+'/dist'))
const comms = require('./comms.server.js')(server)
var user_stocks = require('./user_stocks.js')
comms.on('connection', function(ws){
console.log('comm.connection', ws._socket.remotePort)
// ip ws._socket.remoteAddress
comms.send({ws, data: {'*': user_stocks.stocks} })
})
comms.on('message', function({data, ws}){
if (typeof data.user_stocks === 'object' &&
typeof user_stocks[data.user_stocks[0]] === 'function' &&
typeof data.user_stocks[1] === 'object'
) {
data.user_stocks[1].ip = ws._socket.remoteAddress
user_stocks[data.user_stocks[0]](data.user_stocks[1])
comms.send({ws, data: user_stocks.stocks})
}
})
notify_events = ['add', 'remove', 'set']
notify_events.forEach(function(name){
user_stocks.events.on(name, function(data){
console.log('user_stock:', name, data)
if (name === 'set') {
data = user_stocks.get({name: data})
}
comms.sendAll({
user_stock: [name, data]
})
})
})
user_stocks.init().then(function(){
console.log('user_stocks init')
}).catch(function(err){
console.log('user_stocks init_err', err)
})
server.listen(PORT, function() {
console.log('listening on http://localhost:'+PORT);
})