|
-
Dec 16th, 2010, 07:53 PM
#1
[RESOLVED] Keeping an object per user between pages
Hi,
This seems like an easy task but I cannot figure it out... Perhaps it's cause it's getting late, but what I'm trying simply does not seem to work.
I am developing a website where users can control a gameserver. The most basic actions a user can take are for example viewing a list of all players on the server and kicking/banning players from the server.
The communication with the server happens via UDP, and I am using a Socket object to send and receive arrays of bytes. This is all working just fine.
I have now created a class 'Connection' that holds this Socket object internally, and in the constructor it accepts an IP and password to connect.
The Connection class will have a method for every action the user can take. For example, to retrieve the name of the server, one sends the message "sv_hostname" to the server and parses the reply. The Connection class thus has this method;
Code:
public void GetHostName()
{
return this.GetCommandValue("sv_hostname");
}
private void GetCommandValue(string command)
{
string response = this.SendCommand(command);
return this.ParseCommandResponse(response, command);
}
The GetCommandValue method simply sends the command to the server (with some additional stuff such as the password), waits for a reply, and then parses the reply to return just the value of the command (there is some other stuff returned as well).
Anyway, the structure of my website is pretty simple.
There is a Master page with a menu, which shows links like "Player list", "Server settings", "Messages", basically all linking to child pages that control a server in one way or another.
In the header of the Master page, the game server that the user is currently connected to should be displayed. If there is no connected server, a link to the 'servers' page has to be displayed. Users have to create an account with which they login, and then they can manage a list of servers. A server is simply an IP and a Password which are stored in a database. Users can add/edit and remove servers from their list, and they can then Connect to a server.
Once connected, the 'connected server' in the master page should display that server, and each child page the user visits should be working on that server. If I connect to 'Server1' for example, and then click the 'Player list' link in the menu, it shows me the players in Server1.
Here is a quick graphical presentation of what I want;

I hope this makes it clear.
So basically, whatever child page the user is visiting, I should always have a reference to the connected server. In my case, the connected server is basically an instance of the Connection class I was talking about.
When a user connects to a server, a new Connection instance is created (and stored where???), which I can retrieve from any page to call commands on.
How do I handle this?
It made sense to me to have the Connection object in the master page, which I can then get and set from the child pages (via the MasterPage property). Then, when the master page posts back, I check if there is a Connection object and whether it is connected to a server or not. If it is, I display that server in the header (just a label).
However, that doesn't seem to work the way I want it. I now have this in my master page:
csharp Code:
public partial class SiteMaster : System.Web.UI.MasterPage { private Rcon.Connection _Connection; public Rcon.Connection Connection { get { return _Connection; } } public void SetConnection(Rcon.Connection conn) { _Connection = conn; } protected void Page_Load(object sender, EventArgs e) { if (this.Connection != null && this.Connection.Connected) { lblCurrentServer.Text = String.Format("Connected to {0} ({1}:{2})", this.Connection.GetHostName(), this.Connection.Host, this.Connection.Port); lnkDisconnect.Visible = true; } else { lblCurrentServer.Text = "Not connected."; lnkDisconnect.Visible = false; } } }
I can set the connection from the Servers child page (the page that lists the servers) when the user clicks Connect on a server. Then I want the master page to post back and display the connected server information.
But that doesn't work, since the Connection object will be null again after a postback...
For some reason I think I'm going about things completely wrong...
Well, long post, and I hope I made myself clear, but basically what I'm asking is:
how can I store this Connection object, one per user, and have it persisted between different pages and between post backs?
Thanks for any ideas!
-
Dec 17th, 2010, 09:25 AM
#2
Re: Keeping an object per user between pages
Well, I figured out that I could store it in a session variable. I thought those were different for each page but apparently not... I dunno how secure that is though, I'll make a new thread for that.
-
Dec 18th, 2010, 01:16 PM
#3
Re: [RESOLVED] Keeping an object per user between pages
Hello,
Yes, Session variables are available across pages on your ASP.Net Application. View State variables are available across only the post backs to the current page.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|