private void InitializeClient() { _config = new NetPeerConfiguration("MudServer");//@ string appID _client = new NetClient(_config);//intialize with the config _client.RegisterReceivedCallback(new SendOrPostCallback(RecieveData));//Attach the new thread for server responses _client.Start();//start the socket //need to add logger Console.WriteLine("started the client socket"); try//try to connect to server { //_client.Connect(new System.Net.IPEndPoint(new System.Net.IPAddress(new byte[] { 50, 45, 120, 46 }), 5268)); //_client.Connect(new System.Net.IPEndPoint(new System.Net.IPAddress(new byte[] { 172, 17, 109, 174 }), 5268)); _client.Connect("localhost", 5268); } catch (Exception e)//fail try localhost { _client.Connect("localhost", 5268);//connect the client to the server50.45.120.46 } Console.WriteLine("Requesting Connection to server"); me = new Player(); }
private void RecieveData(object state) { NetIncomingMessage incmsg; NetOutgoingMessage outmsg; //while(true) while ((incmsg = _client.ReadMessage()) != null) { Console.WriteLine("Recieved Data."); switch (incmsg.MessageType) { case NetIncomingMessageType.StatusChanged: Console.WriteLine("_client Status Changed to " + _client.ConnectionStatus.ToString()); if (_client.ConnectionStatus == NetConnectionStatus.Connected) { Console.WriteLine("_client Connected to Server"); outmsg = _client.CreateMessage(); outmsg.Write((byte) Headers.Login);//Requesting login TODO Authorization _client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered); Console.WriteLine("Sent game world request to server."); } else if (_client.ConnectionStatus == NetConnectionStatus.Disconnected) { addText("client is now disconnected"); } else if (_client.ConnectionStatus == NetConnectionStatus.None) { addText("The client is not connected"); } break; case NetIncomingMessageType.Data: Console.WriteLine("Recieved data from server"); byte header = incmsg.ReadByte(); switch (header) { case Headers.WorldUpdate: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Console.WriteLine("recieving game world update"); byte updateType = incmsg.ReadByte(); switch (updateType) { case Headers.PlayerUpdate://Player update players.Clear();//clear the old data int count = incmsg.ReadInt32();//get the count of players for (int i = 0; i < count; i++)//add the updated players to the list { Player play = new Player(); incmsg.ReadAllProperties(play); //read in all the properities into the player //I think it auto serializes if (me.Id == play.Id)//if the unique ids match me(this client) equals that player me = play; players.Add(play);//add it to the player list } break; case Headers.RoomUpdate: var roomString =incmsg.ReadString(); playerRoom.deserialize(roomString); addText(playerRoom.compileDescription(players)); break; case Headers.BothRoomAndPlayer: players.Clear(); int count2 = incmsg.ReadInt32(); for (int i = 0; i < count2; i++) { Player pla = new Player(); try { incmsg.ReadAllProperties(pla); } catch (Exception e) { Console.WriteLine(e.ToString()); } if(pla.Name != string.Empty) if (me.Id == pla.Id) me = pla; players.Add(pla); } try { var rmString = incmsg.ReadString(); playerRoom.deserialize(rmString); } catch (Exception et) { Console.WriteLine(et.ToString()); } break; } addText(playerRoom.compileDescription(players)); Console.WriteLine("Updated world state"); break; //End World update~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ case Headers.Chat: Console.WriteLine(incmsg.PeekString()); var chat_s = incmsg.ReadString(); //addText(incmsg.ReadString()); //Encapsulate this sh#t. Chat_Box.Dispatcher.Invoke(new Action(() => { Chat_Box.Text += chat_s; })); //TextInterface.Document = Text; break; case Headers.Login://just login to world update header if (string.IsNullOrEmpty(me.Name)) { incmsg.ReadAllProperties(me); //addText("login: "******"Data Recycled"); break; } } }