Persistent Property Sample
The functionality of the PersistentProperty
attribute is connected with the PersistentId
of the connection. When a connection is closed all properties that is decorated with the PersistentProperty
attribute will be saved with the PersistentId
as a key. When the client reconnects (with the same PersistentId
) the properties decorated with the PersistentProperty
attribute will be restored.
Sample
We have a controller
Foo
with a single property Bar
. The Bar
property is decorated with the PersistentProperty
attribute.
public class Foo : XSocketController
{
[PersistentProperty]
public int Bar { get; set; }
}
To test this we can use Putty.
1. Connect
Connect with Putty
When connected we pass in foo|1|
If you remember the reserved topics table you will know that 1
opens a controller. The server will then respond with 2
(controller open). We also get a PI (PersistentId
) back. We copy this to use in the reconnect.
2. Set the Bar property to 42
Now set the value of the Bar
property by using the compiler generated set method.
So, foo|set_bar|42
will set the property Bar
to the value 42
3. Reconnect
Close Putty and open it up again, now we pass in the PersistentId
(that we copied) in the handshake.
PuttyProtocol?PersistentId=8853ea4d-e82c-4c01-b029-08175d0724f3
When we call foo|get_bar|
the server returns 42 since the value was persisted between the connections.
Important
The default module for PersistentProperty
stores the values in-memory. It has a timeout that is 30 seconds. If the client does not reconnect within this limit the server will delete the values.
To change this timeout to (for example) 1 minute you can use
XSockets.Core.Common.Globals.Constants.Connection.PropertyStorageTimeoutInSeconds = 60;
If you need full control and "real" persistence you should implement a custom module for the PersistentProperty. A sample is provided in the next section.