So, on the last networking post we had added the ability to view available sessions. Now we are going to try attempt to join a session.
Lets start by editing the SearchScreen.cs and modify the OnSelectEntry function to add the available session as new buttons.
/// <summary>
/// Responds to user menu selections.
/// </summary>
protected override void OnSelectEntry(int entryIndex)
{
int numAvailableSessions = 0;
if (availableSessions != null)
{
if (entryIndex < numAvailableSessions)
{
else
{
}if (availableSessions != null)
{
numAvailableSessions = availableSessions.Count;
}if (entryIndex < numAvailableSessions)
{
LIVESearch = NetworkSession.BeginJoin(availableSessions[entryIndex], new AsyncCallback(LoadLobbyScreen), null);
}else
{
switch (entryIndex)
{
} {
case 0:
Search();
break;
case 1:
}Search();
break;
// If the current selected index is less than the
// number of available sessions then attempt to join
ScreenManager.AddScreen(new XboxLiveScreen(this.networkSessionType));
break;
// number of available sessions then attempt to join
ScreenManager.AddScreen(new XboxLiveScreen(this.networkSessionType));
break;
And then much like we did when we created a session we now have to write the callback function for the joining of the session:
/// <summary>
/// Callback to load the lobby screen with the session you have joined
/// </summary>
void LoadLobbyScreen(IAsyncResult result)
{
if ((LIVESearch != null) && LIVESearch.IsCompleted)
{
}{
NetworkSession networkSession = null;
isEnabled = true;
try
{
catch (NetworkException error)
{
// A valid network session has been created
if (networkSession != null)
{
// Dont need this anymore so set it to null
LIVESearch = null;
}isEnabled = true;
try
{
networkSession = NetworkSession.EndJoin(result);
}catch (NetworkException error)
{
MessageBoxScreen messageBox = new MessageBoxScreen("Failed to join the session");
messageBox.Accepted += FailedMessageBox;
messageBox.Cancelled += FailedMessageBox;
ScreenManager.AddScreen(messageBox);
System.Console.WriteLine("Failed to join the session: " + error.Message);
}messageBox.Accepted += FailedMessageBox;
messageBox.Cancelled += FailedMessageBox;
ScreenManager.AddScreen(messageBox);
System.Console.WriteLine("Failed to join the session: " + error.Message);
// A valid network session has been created
if (networkSession != null)
{
// Set a few properties:
networkSession.AllowHostMigration = true;
networkSession.AllowJoinInProgress = true;
LobbyScreen lobbyScreen = new LobbyScreen(networkSession);
lobbyScreen.ScreenManager = this.ScreenManager;
ScreenManager.AddScreen(lobbyScreen);
}networkSession.AllowHostMigration = true;
networkSession.AllowJoinInProgress = true;
LobbyScreen lobbyScreen = new LobbyScreen(networkSession);
lobbyScreen.ScreenManager = this.ScreenManager;
ScreenManager.AddScreen(lobbyScreen);
// Dont need this anymore so set it to null
LIVESearch = null;
/// <summary>
/// Event handler for when the user selects ok on the network-operation-failed message box.
/// </summary>
void FailedMessageBox(object sender, EventArgs e) { }
And hopefully thats it, we should now be able so join an available session and if successful we should end up in the lobby and be able to see the other player's tags.
No comments:
Post a Comment