protected override void LoadContent() { InputProfiles = new Dictionary <string, InputManager>(); SpriteBatch = new SpriteBatch(Graphics.GraphicsDevice); Rooms = new RoomHandler(); Rng = new Random(); Debug.Assets.InitDebugAssets(); InputProfiles["global_keyboard"] = new InputManager(); GlobalKeyboard = InputProfiles["global_keyboard"]; Rooms.AddRoom(new TestRoom(new Point(1000, 1000), "test_1_fuji")); CurrentRoom = Rooms.GetRoom("test_1_fuji"); CurrentRoom.LoadContent(); CurrentRoom.InitializeRoom(); Rooms.AddRoom(new TestRoom2(new Point(1000, 1000), "test_2_yamato")); Rooms.GetRoom("test_2_yamato").LoadContent(); Rooms.GetRoom("test_2_yamato").InitializeRoom(); CurrentRoom = Rooms.GetRoom("test_2_yamato"); _view = Matrix.Identity; }
async Task SocketSender() { //Console.WriteLine("Begin MessageSender"); var buffer = new byte[BufferSize]; var seg = new ArraySegment <byte>(buffer); for (;;) { //Console.WriteLine("In Loop"); if (this.socket.State != WebSocketState.Open) { break; } var inBuf = await this.socket.ReceiveAsync(seg, CancellationToken.None); if (this.socket.State != WebSocketState.Open) { break; } var jsonStr = System.Text.Encoding.ASCII.GetString(seg.Array.Take(inBuf.Count).ToArray()); if (jsonStr.IndexOf("message") == 9) { //Console.WriteLine("roomHandler.rooms[0].sockets.Count={0}", roomHandler.rooms[0].sockets.Count); //Console.WriteLine("Server received a Message"); var json = JsonConvert.DeserializeObject <DataToReceiveMessage>(jsonStr); var dataToSend = new DataToSendMessage(); dataToSend.type = "message"; dataToSend.username = DBHandler.GetUsername(user_id); dataToSend.message = json.message; dataToSend.timestamp = DateTime.Now.ToString("h:mm tt"); dataToSend.color = DBHandler.GetUserColor(user_id); DBHandler.InsertMessage(dataToSend, roomNumber); await roomHandler.SendMessageToRoom(dataToSend, roomNumber); } else if (jsonStr.IndexOf("connected") == 9) { var json = JsonConvert.DeserializeObject <DataToReceiveConnected>(jsonStr); var userResult = DBHandler.VerifyAuthKey(json.authKey); if (userResult >= 0) { user_id = userResult; await PopulateRoomWithMessages(); await PopulateSidebarWithRooms(); roomHandler.AddSocket(socket, user_id, roomNumber); var dataToSend = new DataToSendBot(); dataToSend.type = "bot"; dataToSend.message = DBHandler.GetUsername(user_id) + " connected!"; await roomHandler.SendDataToRoom(dataToSend, roomNumber); } else { var dataToSend = new DataToSendAuthKey(); dataToSend.type = "authkey"; dataToSend.authKey = ""; await roomHandler.SendDataToSocket(dataToSend, socket); } } else if (jsonStr.IndexOf("changeroom") == 9) { var json = JsonConvert.DeserializeObject <DataToReceiveChangeRoom>(jsonStr); await ChangeRoom(json.roomNumber); } else if (jsonStr.IndexOf("createroom") == 9) { var json = JsonConvert.DeserializeObject <DataToReceiveCreateRoom>(jsonStr); if (DBHandler.RoomNameAvailable(json.roomName)) { roomHandler.AddRoom(json.roomName); await ChangeRoom(DBHandler.GetRoomId(json.roomName)); var dataToSend = DBHandler.GetRoomsData(); await roomHandler.SendDataToAll(dataToSend); } } else if (jsonStr.IndexOf("login") == 9) { var json = JsonConvert.DeserializeObject <DataToReceiveLoginOrCreate>(jsonStr); var dataToSend = new DataToSendAuthKey(); dataToSend.type = "authkey"; if (DBHandler.ValidLogin(json.username, json.password)) { var authKey = SocketHandler.GenerateAuthKey(16); user_id = DBHandler.GetUserId(json.username); DBHandler.InsertAuthKey(authKey, user_id); dataToSend.authKey = authKey; } else { dataToSend.authKey = "invalid login"; } await roomHandler.SendDataToSocket(dataToSend, socket); } else if (jsonStr.IndexOf("createuser") == 9) { var json = JsonConvert.DeserializeObject <DataToReceiveLoginOrCreate>(jsonStr); var dataToSend = new DataToSendAuthKey(); dataToSend.type = "authkey"; if (DBHandler.UsernameAvailable(json.username)) { var authKey = SocketHandler.GenerateAuthKey(16); DBHandler.InsertUser(json.username, json.password); user_id = DBHandler.GetUserId(json.username); DBHandler.InsertAuthKey(authKey, user_id); dataToSend.authKey = authKey; } else { dataToSend.authKey = "taken username"; } await roomHandler.SendDataToSocket(dataToSend, socket); } else { Console.WriteLine("Invalid Data Type"); } } roomHandler.RemoveSocket(socket); }