Download presentation
Presentation is loading. Please wait.
Published byJanel Jefferson Modified over 9 years ago
8
PeerFinder.Role = PeerRole.Client; PeerFinder.Role = PeerRole.Host;
9
PeerFinder Networking Model Host Client
10
PeerFinder HostClient PeerFinder.Role = PeerRole.Client; PeerFinder.ConnectionRequested += ConnectionRequested; PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged; PeerFinder.Start(); PeerFinder.Role = PeerRole.Host; PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged; PeerFinder.Start();
11
Finding players HostClient PeerFinder.Role = PeerRole.Client; PeerFinder.Start(); // Wait for Host to find and invite client. private async void FindPeers() { progressBar.Visibility = Visibility.Visible; try { var peerInfoCollection = await PeerFinder.FindAllPeersAsync(); if (peerInfoCollection.Count > 0) { // Display nearby clients in a list...
12
Connecting players HostClient – incoming connection request private async void ConnectionRequested(object sender, ConnectionRequestedEventArgs e) { List existingInvitations = new List (); existingInvitations.Add(e.PeerInformation.DisplayName);... ReceivedInvitationsHeader.Text = "STOMP invitations :-)"; progressBar.Visibility = Visibility.Collapsed; } private async Task ConnectToPeers(PeerInformation peer) { progressBar.Visibility = Visibility.Visible; StreamSocket s = await PeerFinder.ConnectAsync(peer); ConnectedPeer temp = new ConnectedPeer(peer.DisplayName); connectedPeers[temp] = new SocketReaderWriter(s, this);... startGameButton.Visibility = Visibility.Visible; }
13
Connecting players (continued) HostClient – connect devices // Navigate client to a new page - WaitingForHost... { backButton.Visibility = Visibility.Collapsed; WaitingForHostParameters parameters = (WaitingForHostParameters)navigationParameter; StreamSocket socket = await PeerFinder.ConnectAsync(parameters.peer); pageTitle.Text = "Connected! Waiting for Host...";... } // Host connecting to client...
14
ConnectedPlayers.Items.Add(peer.DisplayName);
15
Tap and connect HostClient – connect devices private async void TriggeredConnectionStateChanged(object sender, TriggeredConnectionStateChangedEventArgs e) { if (e.State == TriggeredConnectState.PeerFound) { // Show indeterminate progress UI } else if (e.State == TriggeredConnectState.Completed) { StreamSocket socket = e.Socket; WaitForHost(socket); } else if (e.State == TriggeredConnectState.Failed) { // Inform challenger that connection failed } private async void TriggeredConnectionStateChanged(object sender, TriggeredConnectionStateChangedEventArgs e) { if (e.State == TriggeredConnectState.PeerFound) { // Show indeterminate progress UI } else if (e.State==TriggeredConnectState.Completed) { socket = new SocketReaderWriter(e.Socket, this); } else if (e.State == TriggeredConnectState.Failed) { // Inform Game Leader that connection failed }
16
Sending/Receiving data - timer HostClient public async void ReadMessage() { // Read incoming message from the socket uint bytesRead = await dataReader.LoadAsync(sizeof(uint)); uint messageLength = dataReader.ReadUInt32(); bytesRead = await dataReader.LoadAsync(messageLength); currentMessage = dataReader.ReadString(messageLength);... } private int ParseMessage() {... case Constants.OpCodeUpdateClientTime: if (gamePage != null) gamePage.UpdateClientTime(words[1]); break; private void UpdateTime(TickOption option) { // Decrement the timer by 1 second foreach (SocketReaderWriter tempsocket in connectedPeers.Values) { string message = string.Format( "{0} {1}", Constants.OpCodeUpdateClientTime, timeLeft.ToString(@"m\:ss")); tempsocket.WriteMessage(message); } }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.