SecondLife.Network Events

From libopenmetaverse - libomv - Developer Wiki

Jump to: navigation, search

Contents

Prerequisites

CSharp Tutorials
Getting Started
How to create a basic libSL bot

Introduction

The following class demonstrates how to listen to SecondLife.Network events.

Network events allow your application to:

  • Determine the success or failure of login and reason for an unexpected logout
  • Get simulator properties
  • Display specific GUI interface such as a progress bar



C# Code

 
using System;
using libsecondlife;
 
namespace SLClientNetwork
{
	class Program
	{
 
		#region Fields
		private static SecondLife client;
		private static string firstname = "First";
		private static string lastname = "Last";
		private static string password = "Password";
		#endregion
 
 
		#region Methods
		static void Main ( string[] args )
		{
 
			// Create new instance of SecondLife class
			client = new SecondLife();
 
			// Listen to SecondLife Network events
			client.Network.OnConnected += new NetworkManager.ConnectedCallback( Network_OnConnected );
			client.Network.OnCurrentSimChanged += new NetworkManager.CurrentSimChangedCallback( Network_OnCurrentSimChanged );
			client.Network.OnDisconnected += new NetworkManager.DisconnectedCallback( Network_OnDisconnected );
			client.Network.OnEventQueueRunning += new NetworkManager.EventQueueRunningCallback( Network_OnEventQueueRunning );
			client.Network.OnLogin += new NetworkManager.LoginCallback( Network_OnLogin );
			client.Network.OnLogoutReply += new NetworkManager.LogoutCallback( Network_OnLogoutReply );
			client.Network.OnSimConnected += new NetworkManager.SimConnectedCallback( Network_OnSimConnected );
			client.Network.OnSimConnecting += new NetworkManager.SimConnectingCallback( Network_OnSimConnecting );
			client.Network.OnSimDisconnected += new NetworkManager.SimDisconnectedCallback( Network_OnSimDisconnected );
 
			// Log in to Second Life Simulator
			if ( !client.Network.Login( firstname, lastname, password, "Test Client", "1.0" ) )
			{
				Console.WriteLine( client.Network.LoginMessage + "\r\n" );
			}
 
			// Make console application wait until user presses a key to quit
			Console.WriteLine( "** Press any key to log out **" );
			Console.Read();
 
			// Log out of Second Life Simulator
			Console.WriteLine( "Logging out..." );
			client.Network.Logout();
 
		}
		#endregion
 
 
		#region SecondLife.Network Events
		/// <summary>
		/// Called any time the login status changes, will eventually return LoginStatus.Success or LoginStatus.Failure
		/// </summary>
		static void Network_OnLogin ( LoginStatus login, string message )
		{
			Console.WriteLine(
				"Network_OnLogin:\n" +
				"\tLogin Status: " + login.ToString() + "\n" +
				"\tMessage: " + message + "\n" );
		}
 
		/// <summary>
		/// Event raised when the client was able to connected successfully.
		/// </summary>
		static void Network_OnConnected ( object sender )
		{
			Console.WriteLine( "Network_OnConnected\n" );
		}
 
		/// <summary>
		/// Triggered when an event queue makes the initial connection
		/// </summary>
		static void Network_OnEventQueueRunning ( Simulator simulator )
		{
			Console.WriteLine(
				"Network_OnEventQueueRunning:\n" +
				"\tSimulator: " + simulator.Name + "\n" );
		}
		
		/// <summary>
		/// Event raised when a before a connection to a simulator is initialized
		/// </summary>
		static bool Network_OnSimConnecting ( Simulator simulator )
		{
			Console.WriteLine(
				"Network_OnSimConnecting:\n" +
				"\tSimulator: " + simulator.Name + "\n" );
 
			return true;
		}
 
		/// <summary>
		/// Event raised when a connection to a simulator is established
		/// </summary>
		static void Network_OnSimConnected ( Simulator simulator )
		{
			Console.WriteLine(
				"Network_OnSimConnected:\n" +
				"\tSimulator: " + simulator.Name + "\n" );
		}
 
		/// <summary>
		/// An event for the connection to a simulator other than the currently occupied one disconnecting
		/// </summary>
		static void Network_OnSimDisconnected ( Simulator simulator, NetworkManager.DisconnectType reason )
		{
			Console.WriteLine(
				"Network_OnSimDisconnected:\n" +
				"\tSimulator: " + simulator.Name + "\n" +
				"\tReason: " + reason.ToString() + "\n" );
		}
 
		/// <summary>
		/// An event for when CurrentSim changes
		/// </summary>
		static void Network_OnCurrentSimChanged ( Simulator PreviousSimulator )
		{
			if ( PreviousSimulator != null )
			{
 
				Console.WriteLine(
					"Network_OnCurrentSimChanged:\n" +
					"\tPrevious Simulator: " + PreviousSimulator.Name + "\n" );
 
			}
		}
 
		/// <summary>
		/// Event raised when a logout is confirmed by the simulator
		/// </summary>
		static void Network_OnLogoutReply ( System.Collections.Generic.List<LLUUID> inventoryItems )
		{
			Console.WriteLine( "Network_OnLogoutReply" );
 
			foreach ( LLUUID item in inventoryItems )
			{
				Console.WriteLine( "\tInventory Item: " + item.UUID );
			}
 
			Console.WriteLine( "" );
		}
 
		/// <summary>
		/// An event for being logged out either through client request, server forced, or network error
		/// </summary>
		static void Network_OnDisconnected ( NetworkManager.DisconnectType reason, string message )
		{
 
			Console.WriteLine(
				"Network_OnDisconnected:\n" +
				"\tReason: " + reason.ToString() + "\n" +
				"\tMessage: " + message + "\n" );
 
		}
		#endregion
 
	}
}
 



Typical Output

Network_OnLogin:
        Login Status:ConnectingToSim
        Message: Connecting to simulator...

Network_OnSimConnecting:
        Simulator:

INFO [firstname lastname]: Connecting to (8.2.33.234:13000)
INFO [firstname lastname]: Received a region handshake for Sandbox Wanderton (8.2.33.234:13000)
Network_OnSimConnected:
        Simulator: Sandbox Wanderton

-- Press any key to log out --
Network_OnLogin:
        Login Status:Success
        Message: Have questions about Second Life? Not sure where to ask? Check out the Support page! http:// secondlife.com/support

Network_OnConnected

DEBUG [firstname lastname]: No handler registered for packet event AvatarAppearance
Network_OnEventQueueRunning:
        Simulator: Sandbox Wanderton

Network_OnSimConnecting:
        Simulator:

INFO [firstname lastname]: Connecting to (216.82.27.192:13000)
INFO [firstname lastname]: Received a region handshake for Sandbox Newcomb (216.82.27.192:13000)
Network_OnSimConnected:
        Simulator: Sandbox Newcomb

INFO [firstname lastname]: Got EstablishAgentCommunication for Sandbox Newcomb (216.82.27.192:13000)
Network_OnEventQueueRunning:
        Simulator: Sandbox Newcomb

DEBUG [firstname lastname]: No handler registered for packet event RebakeAvatarTextures

(User pressed the Enter key to log out)

Logging out...
INFO [firstname lastname]: Logging out
Network_OnLogoutReply
        Inventory Item: 00000000-0000-0000-0000-000000000000

INFO [firstname lastname]: NetworkManager shutdown initiated
INFO [firstname lastname]: Caps system for Sandbox Newcomb (216.82.27.192:13000) is aborting
Network_OnSimDisconnected:
        Simulator: Sandbox Newcomb
        Reason: ClientInitiated

INFO [firstname lastname]: Caps system for Sandbox Wanderton (8.2.33.234:13000) is aborting
Network_OnSimDisconnected:
        Simulator: Sandbox Wanderton
        Reason: ClientInitiated

Network_OnDisconnected:
        Reason: ClientInitiated
        Message:

Author

Drew Lorefield