-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (55 loc) · 1.78 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
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from './webpack.server.config';
import crypto from 'crypto';
import { createStore, combineReducers } from 'redux';
import { allCombined } from './app/reducers/root-reducer';
let store = createStore(allCombined);
var express = require('express');
var bodyParser = require('body-parser').json();
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(
compiler,
{
noInfo: true,
path: webpackConfig.output.path,
publicPath: webpackConfig.output.publicPath
}
));
app.use(webpackHotMiddleware(compiler))
app.use('/static', express.static('dist'))
app.get('*', (req, res) => {
res.sendFile(webpackConfig.output.path + '/index.html');
})
io.on('connection', (socket) => {
socket.emit('initial state', store.getState())
// as a dev tool there's a button to flush down the state
socket.on('HARD_RESET', (action) => {
store = createStore(allCombined);
socket.emit('initial state', store.getState())
})
// all global broadcasting messages, client input bus
socket.on('action', (action) => {
console.log()
console.log("ACTION")
console.log(action)
// save in server store
store.dispatch(action)
// broadcast to all listeners
socket.broadcast.emit("action", action)
console.log()
console.log("SERVERSIDE STORE STATE:")
console.log(store.getState())
});
// todo: after disconnect should purge all data about user
socket.on('disconnect', () => {
// console.log('user disconnected purge all data')
})
});
server.listen(8080, function () {
console.log('Listening on port 8080!');
});