diff options
author | yipstanley <stanley_yip@brown.edu> | 2019-02-11 00:28:48 -0500 |
---|---|---|
committer | yipstanley <stanley_yip@brown.edu> | 2019-02-11 00:28:48 -0500 |
commit | ab6fe5dbe625bca862558557224d6b5f8d2e5f1d (patch) | |
tree | b85114a770ed44a6ac2120dbb8dcfc0bdc48e5ff /src | |
parent | 5d081dc8ea638f9729058bfe7a310cebbd9f5581 (diff) |
asdfkjlasdfljkagit add -A!
Diffstat (limited to 'src')
-rw-r--r-- | src/Utils.ts | 18 | ||||
-rw-r--r-- | src/client/Server.ts | 11 | ||||
-rw-r--r-- | src/client/SocketStub.ts | 30 | ||||
-rw-r--r-- | src/server/Message.ts | 35 | ||||
-rw-r--r-- | src/server/index.ts | 51 | ||||
-rw-r--r-- | src/server/server.ts | 31 |
6 files changed, 119 insertions, 57 deletions
diff --git a/src/Utils.ts b/src/Utils.ts index cc1d8f6c6..f735ddc73 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1,5 +1,7 @@ import v4 = require('uuid/v4'); import v5 = require("uuid/v5"); +import { Socket } from 'socket.io'; +import { Message } from './server/Message'; export class Utils { @@ -19,4 +21,20 @@ export class Utils { return { scale, translateX, translateY }; } + + public static Emit<T>(socket: Socket | SocketIOClient.Socket, message: Message<T>, args: T) { + socket.emit(message.Message, args); + } + + public static EmitCallback<T>(socket: Socket | SocketIOClient.Socket, message: Message<T>, args: T, fn: (args: any) => any) { + socket.emit(message.Message, args, fn); + } + + public static AddServerHandler<T>(socket: Socket, message: Message<T>, handler: (args: T) => any) { + socket.on(message.Message, handler); + } + + public static AddServerHandlerCallback<T>(socket: Socket, message: Message<T>, handler: (args: [T, (res: any) => any]) => any) { + socket.on(message.Message, (arg: T, fn: (res: any) => any) => handler([arg, fn])); + } }
\ No newline at end of file diff --git a/src/client/Server.ts b/src/client/Server.ts index 66ba92497..db6bf992c 100644 --- a/src/client/Server.ts +++ b/src/client/Server.ts @@ -9,7 +9,7 @@ import { MessageStore } from "./../server/Message"; export class Server { private static ClientFieldsCached: ObservableMap<FIELD_ID, Field | FIELD_WAITING> = new ObservableMap(); - static Socket: SocketIOClient.Socket = OpenSocket("http://localhost:8080") + static Socket: SocketIOClient.Socket = OpenSocket("http://localhost:1234") static GUID: string = Utils.GenerateGuid() // Retrieves the cached value of the field and sends a request to the server for the real value (if it's not cached). @@ -20,8 +20,7 @@ export class Server { this.ClientFieldsCached.set(fieldid, FieldWaiting); //simulating a server call with a registered callback action SocketStub.SEND_FIELD_REQUEST(fieldid, - action((field: Field) => callback(Server.cacheField(field))), - hackTimeout); + action((field: Field) => callback(Server.cacheField(field)))); } else if (this.ClientFieldsCached.get(fieldid) != FieldWaiting) { callback(this.ClientFieldsCached.get(fieldid) as Field); } @@ -53,6 +52,10 @@ export class Server { SocketStub.SEND_SET_FIELD(field, value); } + static connected(message: string) { + Server.Socket.emit(MessageStore.Bar.Message, Server.GUID); + } + @action private static cacheField(clientField: Field) { var cached = this.ClientFieldsCached.get(clientField.Id); @@ -64,3 +67,5 @@ export class Server { return this.ClientFieldsCached.get(clientField.Id) as Field; } } + +Server.Socket.on(MessageStore.Foo.Message, Server.connected);
\ No newline at end of file diff --git a/src/client/SocketStub.ts b/src/client/SocketStub.ts index 58dedbf82..3b1401632 100644 --- a/src/client/SocketStub.ts +++ b/src/client/SocketStub.ts @@ -2,6 +2,9 @@ import { Field, FIELD_ID } from "../fields/Field" import { Key, KeyStore } from "../fields/Key" import { ObservableMap, action } from "mobx"; import { Document } from "../fields/Document" +import { MessageStore, SetFieldArgs, GetFieldArgs } from "../server/Message"; +import { Utils } from "../Utils"; +import { Server } from "./Server"; export class SocketStub { @@ -17,25 +20,14 @@ export class SocketStub { // server stores stripped down document (w/ only field id proxies) in the field store this.FieldStore.set(document.Id, new Document(document.Id)); document.fields.forEach((f, key) => (this.FieldStore.get(document.Id) as Document)._proxies.set(key, (f as Field).Id)); - } - - public static SEND_FIELD_REQUEST(fieldid: FIELD_ID, callback: (field: Field) => void, timeout: number) { - - if (timeout < 0)// this is a hack to make things easier to setup until we have a server... won't be neededa fter that. - callback(this.FieldStore.get(fieldid) as Field); - else { // actual logic here... - - // Send a request for fieldid to the server - // ...SOCKET(RETRIEVE_FIELD, fieldid) - - // server responds (simulated with a timeout) and the callback is invoked - setTimeout(() => - - // when the field data comes back, call the callback() function - callback(this.FieldStore.get(fieldid) as Field), + // Server.Socket.emit(MessageStore.AddDocument.Message, document) + } - timeout); + public static SEND_FIELD_REQUEST(fieldid: FIELD_ID, callback: (field: Field) => void) { + if (fieldid) { + let args: GetFieldArgs = new GetFieldArgs(fieldid) + Utils.EmitCallback(Server.Socket, MessageStore.GetField, args, (field: Field) => callback(field)) } } @@ -72,7 +64,7 @@ export class SocketStub { // ...SOCKET(SET_FIELD, field id, serialized field value) // Server updates the value of the field in its fieldstore - if (this.FieldStore.get(field.Id)) - this.FieldStore.get(field.Id)!.TrySetValue(value); + let msg: SetFieldArgs = new SetFieldArgs(field.Id as string, value) + Utils.Emit(Server.Socket, MessageStore.SetField, msg) } } diff --git a/src/server/Message.ts b/src/server/Message.ts index d5b9b4612..15329249d 100644 --- a/src/server/Message.ts +++ b/src/server/Message.ts @@ -1,8 +1,10 @@ import { Utils } from "../Utils"; +import { FIELD_ID, Field } from "../fields/Field"; -export class Message { +export class Message<T> { private name: string; private guid: string; + readonly ArgsCtor: new (...args: any) => T; get Name(): string { return this.name; @@ -12,9 +14,10 @@ export class Message { return this.guid } - constructor(name: string) { + constructor(name: string, ctor: new (...args: any) => T) { this.name = name; this.guid = Utils.GenerateDeterministicGuid(name) + this.ArgsCtor = ctor; } GetValue() { @@ -22,6 +25,32 @@ export class Message { } } +class TestMessageArgs { + hello: string = ""; +} + +export class SetFieldArgs { + field: string; + value: any; + + constructor(f: string, v: any) { + this.field = f + this.value = v + } +} + +export class GetFieldArgs { + field: string; + + constructor(f: string) { + this.field = f + } +} + export namespace MessageStore { - export const Handshake = new Message("Handshake"); + export const Foo = new Message("Foo", String); + export const Bar = new Message("Bar", String); + export const AddDocument = new Message("Add Document", TestMessageArgs); + export const SetField = new Message("Set Field", SetFieldArgs) + export const GetField = new Message("Get Field", GetFieldArgs) }
\ No newline at end of file diff --git a/src/server/index.ts b/src/server/index.ts index 640ad8180..b9c587892 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -4,10 +4,18 @@ 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 } 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'; const config = require('../../webpack.config') const compiler = webpack(config) const port = 1050; // default port to listen +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')); @@ -26,4 +34,45 @@ app.use(whm(compiler)) // start the Express server app.listen(port, () => { console.log(`server started at http://localhost:${port}`); -});
\ No newline at end of file +}) + +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.AddDocument, addDocument) + Utils.AddServerHandler(socket, MessageStore.SetField, setField) + // socket.on(MessageStore.SetField.Message, setField) + Utils.AddServerHandlerCallback(socket, MessageStore.GetField, getField) +}) + +function barReceived(guid: String) { + clients[guid.toString()] = new Client(guid.toString()); +} + +function addDocument(document: Document) { + +} + +function setField(newValue: SetFieldArgs) { + if (FieldStore.get(newValue.field)) { + FieldStore.get(newValue.field)!.TrySetValue(newValue.value); + } +} + +function getField([fieldRequest, callback]: [GetFieldArgs, (field: Field) => void]) { + let fieldid: string = fieldRequest.field + callback(FieldStore.get(fieldid) as Field) + console.log(fieldid) +} + +server.listen(1234); +console.log("listening on port 1234");
\ No newline at end of file diff --git a/src/server/server.ts b/src/server/server.ts deleted file mode 100644 index db58ed662..000000000 --- a/src/server/server.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { MessageStore } from "./Message"; - -// const express = require("express") -// const path = require("path") - -// const app = express(); -// app.set("port", process.env.PORT || 3000); - -// var http = require('http').Server(app); - -// app.get('/', function (req: any, res: any) { -// res.sendFile(path.resolve("./deploy/index.html")) -// }) - -// const server = http.listen(3000, function () { -// console.log("Listening on *:3000") -// }) - -const server = require("socket.io")(); -var clients = []; - -server.on("connection", function (socket: any) { - console.log("a user has connected") - - socket.emit(MessageStore.Handshake.Message, "handshake received") - - clients.push(socket) -}) - -server.listen(8080); -console.log("listening on port 8080")
\ No newline at end of file |