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
|
import * as express from 'express'
const app = express()
import * as webpack from 'webpack'
import * as wdm from 'webpack-dev-middleware';
import * as whm from 'webpack-hot-middleware';
import * as path from 'path'
import { MessageStore, Message, SetFieldArgs, GetFieldArgs, Transferable } from "./Message";
import { Client } from './Client';
import { Socket } from 'socket.io';
import { Utils } from '../Utils';
import { ObservableMap } from 'mobx';
import { FIELD_ID, Field } from '../fields/Field';
import { Database } from './database';
import { ServerUtils } from './ServerUtil';
import { ObjectID } from 'mongodb';
const config = require('../../webpack.config')
const compiler = webpack(config)
const port = 1050; // default port to listen
const serverPort = 1234;
let FieldStore: ObservableMap<FIELD_ID, Field> = new ObservableMap();
// define a route handler for the default home page
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, '../../deploy/index.html'));
});
app.get("/hello", (req, res) => {
res.send("<p>Hello</p>");
})
app.use(wdm(compiler, {
publicPath: config.output.publicPath
}))
app.use(whm(compiler))
// start the Express server
app.listen(port, () => {
console.log(`server started at http://localhost:${port}`);
})
const server = require("socket.io")();
interface Map {
[key: string]: Client;
}
let clients: Map = {}
server.on("connection", function (socket: Socket) {
console.log("a user has connected")
Utils.Emit(socket, MessageStore.Foo, "handshooken")
Utils.AddServerHandler(socket, MessageStore.Bar, barReceived)
Utils.AddServerHandler(socket, MessageStore.SetField, setField)
Utils.AddServerHandlerCallback(socket, MessageStore.GetField, getField)
})
function barReceived(guid: String) {
clients[guid.toString()] = new Client(guid.toString());
Database.Instance.print()
}
function addDocument(document: Document) {
}
function setField(newValue: Transferable) {
console.log(newValue._id)
if (Database.Instance.getDocument(newValue._id)) {
Database.Instance.update(newValue._id, newValue)
}
else {
Database.Instance.insert(newValue)
}
}
function getField([fieldRequest, callback]: [GetFieldArgs, (field: Field) => void]) {
let fieldid: string = fieldRequest.field
let result: string | undefined = Database.Instance.getDocument(new ObjectID(fieldid))
if (result) {
let fromJson: Field = ServerUtils.FromJson(result)
}
}
server.listen(serverPort);
console.log(`listening on port ${serverPort}`);
|