<p>A session object has its own <aclass="reference internal"href="Command-Sets.html"><spanclass="doc std std-doc">cmdset</span></a>, usually the “unloggedin” cmdset. This is what is used to show the login screen and to handle commands to create a new account (or <aclass="reference internal"href="Accounts.html"><spanclass="doc std std-doc">Account</span></a> in evennia lingo) read initial help and to log into the game with an existing account. A session object can either be “logged in” or not. Logged in means that the user has authenticated. When this happens the session is associated with an Account object (which is what holds account-centric stuff). The account can then in turn puppet any number of objects/characters.</p>
<p>A Session is not <em>persistent</em> - it is not a <aclass="reference internal"href="Typeclasses.html"><spanclass="doc std std-doc">Typeclass</span></a> and has no connection to the database. The Session will go away when a user disconnects and you will lose any custom data on it if the server reloads. The <codeclass="docutils literal notranslate"><spanclass="pre">.db</span></code> handler on Sessions is there to present a uniform API (so you can assume <codeclass="docutils literal notranslate"><spanclass="pre">.db</span></code> exists even if you don’t know if you receive an Object or a Session), but this is just an alias to <codeclass="docutils literal notranslate"><spanclass="pre">.ndb</span></code>. So don’t store any data on Sessions that you can’t afford to lose in a reload.</p>
<p>Here are some important properties available on (Server-)Sessions</p>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">sessid</span></code> - The unique session-id. This is an integer starting from 1.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">address</span></code> - The connected client’s address. Different protocols give different information here.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">logged_in</span></code> - <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> if the user authenticated to this session.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">account</span></code> - The <aclass="reference internal"href="Accounts.html"><spanclass="doc std std-doc">Account</span></a> this Session is attached to. If not logged in yet, this is <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">puppet</span></code> - The <aclass="reference internal"href="Objects.html"><spanclass="doc std std-doc">Character/Object</span></a> currently puppeted by this Account/Session combo. If not logged in or in OOC mode, this is <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">db</span></code> - As noted above, Sessions don’t have regular Attributes. This is an alias to <codeclass="docutils literal notranslate"><spanclass="pre">ndb</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">cmdset</span></code> - The Session’s <aclass="reference internal"href="Command-Sets.html"><spanclass="doc std std-doc">CmdSetHandler</span></a></p></li>
</ul>
<p>Session statistics are mainly used internally by Evennia.</p>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">conn_time</span></code> - How long this Session has been connected</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">cmd_last</span></code> - Last active time stamp. This will be reset by sending <codeclass="docutils literal notranslate"><spanclass="pre">idle</span></code> keepalives.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">cmd_last_visible</span></code> - last active time stamp. This ignores <codeclass="docutils literal notranslate"><spanclass="pre">idle</span></code> keepalives and representes the
last time this session was truly visibly active.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">cmd_total</span></code> - Total number of Commands passed through this Session.</p></li>
<p>When you use <codeclass="docutils literal notranslate"><spanclass="pre">msg()</span></code> to return data to a user, the object on which you call the <codeclass="docutils literal notranslate"><spanclass="pre">msg()</span></code> matters. The
<codeclass="docutils literal notranslate"><spanclass="pre">MULTISESSION_MODE</span></code> also matters, especially if greater than 1.</p>
<p>For example, if you use <codeclass="docutils literal notranslate"><spanclass="pre">account.msg("hello")</span></code> there is no way for evennia to know which session it
should send the greeting to. In this case it will send it to all sessions. If you want a specific
session you need to supply its session to the <codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code> call (<codeclass="docutils literal notranslate"><spanclass="pre">account.msg("hello",</span><spanclass="pre">session=mysession)</span></code>).</p>
<p>On the other hand, if you call the <codeclass="docutils literal notranslate"><spanclass="pre">msg()</span></code> message on a puppeted object, like
<codeclass="docutils literal notranslate"><spanclass="pre">character.msg("hello")</span></code>, the character already knows the session that controls it - it will
cleverly auto-add this for you (you can specify a different session if you specifically want to send
stuff to another session).</p>
<p>Finally, there is a wrapper for <codeclass="docutils literal notranslate"><spanclass="pre">msg()</span></code> on all command classes: <codeclass="docutils literal notranslate"><spanclass="pre">command.msg()</span></code>. This will
transparently detect which session was triggering the command (if any) and redirects to that session
(this is most often what you want). If you are having trouble redirecting to a given session,
<codeclass="docutils literal notranslate"><spanclass="pre">command.msg()</span></code> is often the safest bet.</p>
<p>You can get the <codeclass="docutils literal notranslate"><spanclass="pre">session</span></code> in two main ways:</p>
<ulclass="simple">
<li><p><aclass="reference internal"href="Accounts.html"><spanclass="doc std std-doc">Accounts</span></a> and <aclass="reference internal"href="Objects.html"><spanclass="doc std std-doc">Objects</span></a> (including Characters) have a <codeclass="docutils literal notranslate"><spanclass="pre">sessions</span></code> property.
This is a <em>handler</em> that tracks all Sessions attached to or puppeting them. Use e.g.
<codeclass="docutils literal notranslate"><spanclass="pre">accounts.sessions.get()</span></code> to get a list of Sessions attached to that entity.</p></li>
<li><p>A Command instance has a <codeclass="docutils literal notranslate"><spanclass="pre">session</span></code> property that always points back to the Session that triggered
it (it’s always a single one). It will be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> if no session is involved, like when a mob or
<h3>Customizing the Session object<aclass="headerlink"href="#customizing-the-session-object"title="Permalink to this headline">¶</a></h3>
<p>When would one want to customize the Session object? Consider for example a character creation system: You might decide to keep this on the out-of-character level. This would mean that you create the character at the end of some sort of menu choice. The actual char-create cmdset would then normally be put on the account. This works fine as long as you are <codeclass="docutils literal notranslate"><spanclass="pre">MULTISESSION_MODE</span></code> below 2. For higher modes, replacing the Account cmdset will affect <em>all</em> your connected sessions, also those not involved in character creation. In this case you want to instead put the char-create cmdset on the Session level - then all other sessions will keep working normally despite you creating a new character in one of them.</p>
<p>By default, the session object gets the <codeclass="docutils literal notranslate"><spanclass="pre">commands.default_cmdsets.UnloggedinCmdSet</span></code> when the user first connects. Once the session is authenticated it has <em>no</em> default sets. To add a “logged-in” cmdset to the Session, give the path to the cmdset class with <codeclass="docutils literal notranslate"><spanclass="pre">settings.CMDSET_SESSION</span></code>. This set
<p>To customize further you can completely override the Session with your own subclass. To replace the default Session class, change <codeclass="docutils literal notranslate"><spanclass="pre">settings.SERVER_SESSION_CLASS</span></code> to point to your custom class. This is a dangerous practice and errors can easily make your game unplayable. Make sure to take heed of the <aclass="reference internal"href="../api/evennia.server.session.html#evennia-server-session"><spanclass="std std-ref">original</span></a> and make your changes carefully.</p>
<p>Evennia is split into two parts, the <aclass="reference internal"href="Portal-And-Server.html"><spanclass="doc std std-doc">Portal and the Server</span></a>. Each side tracks its own Sessions, syncing them to each other.</p>
<p>The “Session” we normally refer to is actually the <codeclass="docutils literal notranslate"><spanclass="pre">ServerSession</span></code>. Its counter-part on the Portal
side is the <codeclass="docutils literal notranslate"><spanclass="pre">PortalSession</span></code>. Whereas the server sessions deal with game states, the portal session
deals with details of the connection-protocol itself. The two are also acting as backups of critical
<p>New Account connections are listened for and handled by the Portal using the [protocols](Portal-And- Server) it understands (such as telnet, ssh, webclient etc). When a new connection is established, a <codeclass="docutils literal notranslate"><spanclass="pre">PortalSession</span></code> is created on the Portal side. This session object looks different depending on which protocol is used to connect, but all still have a minimum set of attributes that are generic to all sessions.</p>
<p>These common properties are piped from the Portal, through the AMP connection, to the Server, which is now informed a new connection has been established. On the Server side, a <codeclass="docutils literal notranslate"><spanclass="pre">ServerSession</span></code> object is created to represent this. There is only one type of <codeclass="docutils literal notranslate"><spanclass="pre">ServerSession</span></code>; It looks the same regardless of how the Account connects.</p>
<p>From now on, there is a one-to-one match between the <codeclass="docutils literal notranslate"><spanclass="pre">ServerSession</span></code> on one side of the AMP
connection and the <codeclass="docutils literal notranslate"><spanclass="pre">PortalSession</span></code> on the other. Data arriving to the Portal Session is sent on to
its mirror Server session and vice versa.</p>
<p>During certain situations, the portal- and server-side sessions are
“synced” with each other:</p>
<ulclass="simple">
<li><p>The Player closes their client, killing the Portal Session. The Portal syncs with the Server to
make sure the corresponding Server Session is also deleted.</p></li>
<li><p>The Player quits from inside the game, killing the Server Session. The Server then syncs with the
Portal to make sure to close the Portal connection cleanly.</p></li>
<li><p>The Server is rebooted/reset/shutdown - The Server Sessions are copied over (“saved”) to the
Portal side. When the Server comes back up, this data is returned by the Portal so the two are again
in sync. This way an Account’s login status and other connection-critical things can survive a
server reboot (assuming the Portal is not stopped at the same time, obviously).</p></li>
<codeclass="docutils literal notranslate"><spanclass="pre">sessionhandler</span></code>) so they can relay data. See <aclass="reference internal"href="../Concepts/Protocols.html"><spanclass="doc std std-doc">protocols</span></a> for more info on building new protocols.</p>
<p>To get all Sessions in the game (i.e. all currently connected clients), you access the server-side Session handler, which you get by</p>
<div><p>Note: The <codeclass="docutils literal notranslate"><spanclass="pre">SESSION_HANDLER</span></code> singleton has an older alias <codeclass="docutils literal notranslate"><spanclass="pre">SESSIONS</span></code> that is commonly seen in various places as well.</p>
<p>See the <aclass="reference internal"href="../api/evennia.server.sessionhandler.html#evennia-server-sessionhandler"><spanclass="std std-ref">sessionhandler.py</span></a> module for details on the capabilities of the <codeclass="docutils literal notranslate"><spanclass="pre">ServerSessionHandler</span></code>.</p>