-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
199 lines (159 loc) · 4.85 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// @flow
'use strict';
import express from 'express';
import graphQLHTTP from 'express-graphql';
import path from 'path';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import {Schema} from './data/schema';
import socket from 'socket.io';
import http from 'http';
import NodeSocket from './NodeSocket';
import config from './webpack.config';
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const autoprefixer = require('autoprefixer');
const APP_PORT = 3000;
const GRAPHQL_PORT = 8080;
const SOCKET_PORT = 3001;
// Expose a GraphQL endpoint
var graphQLServer = express();
var socketserver = new http.Server(graphQLServer);
var io = socket(socketserver, {
path: '/napi'
});
const rooms = {};
var latestConnection = 0;
const leaveRoom = (socket, nodeId, myRooms, myConnection)=>{
if(!rooms[nodeId]){
return;
throw `"${nodeId}" does not exist`;
}
if(!myRooms[nodeId]){
return;
throw `you are not joined to "${nodeId}"`;
}
delete myRooms[nodeId];
delete rooms[nodeId].connections[myConnection];
if(!--rooms[nodeId].connectionCount){
rooms[nodeId].node.destroy();
delete rooms[nodeId];
}
socket.leave(nodeId);
};
io.on('connection', (mySocket)=> {
const myRooms = {};
const myConnection = latestConnection++;
mySocket.on('join', (nodeId)=> {
console.log('join:', nodeId)
if(myRooms[nodeId]){
return;
}
myRooms[nodeId] = true;
(rooms[nodeId] || (rooms[nodeId] = {
node: new NodeSocket(nodeId, io),
connectionCount: 0,
connections: {}})).connections[myConnection] = true;
mySocket.join(nodeId);
rooms[nodeId].connectionCount++;
if(rooms[nodeId].node.lastValue){
console.log('sending update...');
mySocket.emit('update', rooms[nodeId].node.lastValue );
}
});
mySocket.on('leave', (nodeId) => leaveRoom(mySocket, nodeId, myRooms, myConnection));
mySocket.on('disconnect', function(){
for(const node of Object.keys(myRooms)) {
leaveRoom(mySocket, node, myRooms, myConnection);
}
});
});
const port: number = Number(process.env.PORT);
if(process.env.NODE_ENV !== 'production') {
// $FlowIgnore: dunno why this doesn't check...
socketserver.listen(SOCKET_PORT, function(){
console.log('socket io on *:' + SOCKET_PORT);
});
graphQLServer.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema
}));
graphQLServer.listen( GRAPHQL_PORT, function(){
console.log('graphql on *:' + GRAPHQL_PORT);
});
const app = new WebpackDevServer(webpack(config), {
contentBase: '/public/',
proxy: {
'/graphql': {target: `http://localhost:${GRAPHQL_PORT}`},
'/napi/*': {
target: `ws://localhost:${SOCKET_PORT}/napi`,
ws: true
}
},
publicPath: config.output.publicPath,
stats: {colors: true},
hot: true,
historyApiFallback: true
});
// Serve static resources
app.use('/', express.static(path.resolve(__dirname, 'public')));
app.listen(APP_PORT, () => {
console.log(`App is now running on http://localhost:${APP_PORT}`);
});
} else {
/*
// Serve the Relay app
var compiler = webpack({
entry: path.resolve(__dirname, 'js', 'app.js'),
plugins: [
new ExtractTextPlugin('example.css', { allChunks: true }), // compiled css (single file only)
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
],
module: {
loaders: [
{
test: /(\.js|\.jsx)$/,
exclude: /(node_modules)/,
loaders: ['react-hot', 'babel'],
//query: {
// presets:['es2015','react']
//}
}, {
test: /(\.scss|\.css)$/,
loader: ExtractTextPlugin.extract('style', 'css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss!sass?sourceMap')
}
]
},
postcss: [autoprefixer],
output: {filename: 'app.js', path: 'build2/js'}
});
//console.log("running compiler");
compiler.run((err, stats)=> {
console.log("compile complete", err, stats);
});
*/
graphQLServer.use('/graphql', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema
}));
graphQLServer.use('/', express.static('build2'));
graphQLServer.get('/*', function(req, res){
res.sendFile(__dirname + '/build2/index.html');
});
const listenPort: number = (port || GRAPHQL_PORT || 8080);
// $FlowIgnore: dunno why this doesn't check...
socketserver.listen(listenPort, function(){
console.log('listening on *:' + port || GRAPHQL_PORT || 8080);
});
}
// graphQLServer.listen(process.env.PORT || GRAPHQL_PORT , () => console.log(
// `GraphQL Server is now running on http://localhost:${process.env.PORT || GRAPHQL_PORT}`
// ));