public void TestAddEntity() { var history = new History(); var eventx = new SpatialEntity("foo", "bar", new Vector3D(), Quaternion.Identity); history.ApplyEvent(eventx); history.Head.Entities["foo"].Should().BeSameAs(eventx); }
public Listener(BlockingCollection<CommandPending> pendingCommands, BlockingCollection<CommandResult> commandResults, History history) { var server = new WebSocketServer("ws://localhost:8181"); var commandMap = new Dictionary<int, IWebSocketConnection>(); int commandID = 0; var userMap = new Dictionary<IWebSocketConnection, User>(); history.EventApplied += (sender, e) => Broadcast(e); server.Start(socket => { socket.OnOpen = () => { Console.WriteLine("Open!"); allSockets.Add(socket); // send the current world // TODO: race condition if event fires before world is sent foreach (var entity in history.Head.Entities.Select(x => x.Value)) { Send(socket, entity); } }; socket.OnClose = () => { Console.WriteLine("Close!"); allSockets.Remove(socket); User user; if (userMap.TryGetValue(socket, out user)) { var remove = new Remove() { name = user.Name }; pendingCommands.TryAdd(new CommandPending(commandID, new User(), remove)); commandMap.Add(commandID++, socket); } }; socket.OnMessage = message => { Console.WriteLine(message); try { var update = JsonConvert.DeserializeObject<Update>(message); userMap[socket] = new User() { Name = update.name }; pendingCommands.TryAdd(new CommandPending(commandID, new User(), update)); commandMap.Add(commandID++, socket); } catch (Exception ex) { Console.WriteLine(ex); } }; }); CommandResult result; while (commandResults.TryTake(out result)) { IWebSocketConnection socket; if (commandMap.TryGetValue(result.Id, out socket)) { Send(socket, result); commandMap.Remove(result.Id); } } }
static void Main(string[] args) { var commands = new BlockingCollection<CommandPending>(); var results = new BlockingCollection<CommandResult>(); var history = new History(); var listener = new Listener(commands, results, history); var ticker = new Ticker(commands, results, history); var task = new Task(ticker.ProcessCommands); task.Start(); var input = Console.ReadLine(); while (input != "exit") { listener.Broadcast(input); input = Console.ReadLine(); } }
public Ticker(BlockingCollection<CommandPending> pending, BlockingCollection<CommandResult> results, History history) { pendingCommands = pending; commandResults = results; worldHistory = history; }