Server: Events
In this tutorial you will learn how to tie into events from your WebSync Server request
handler. We will use a simple publish hook to modify messages on-the-fly.
Prerequisites
Configuring your project
Before you can start coding, you need to have the correct project references.
- Add a reference to
FM.WebSync.Core (available as part of the WebSync Server downloads).
- Add a reference to
FM.WebSync.Server (available as part of the WebSync Server downloads).
- Add a reference to
System.Runtime.Serialization (available as part of the .NET Framework).
Modifying messages in a handler method
Methods can be added to the request handler that allow pre- and post-processing of messages
and other server-side events.
-
Modify the request handler created in the WebSync Server: Basic tutorial.
- Define a
Data class for the purpose of deserialization.
- Add a method called
PrePublish.
- In the method, iterate over the messages and modify the
data property.
using System.Runtime.Serialization;
using FM.WebSync.Server;
using FM.WebSync.Core;
namespace Tutorials
{
[HandlerProvider(ProviderType.Basic)]
public class RequestHandler : Handler<RequestHandler>
{
[DataContract]
private class Data
{
[DataMember(Name = "text")]
public string Text { get; set; }
}
[HandlerEvent(HandlerEventType.BeforePublish, HandlerEventContinue.OnComplete)]
public static void PrePublish(object sender, HandlerEventArgs e)
{
foreach (Message message in e.Messages)
{
Data data = JSON.Deserialize<Data>(message.Data);
data.Text = data.Text.ToUpperInvariant();
message.Data = JSON.Serialize(data);
}
e.Complete(); // continue server-side processing
}
}
}
Testing
Open up your client page in a couple windows and watch as the publications are upper-cased before delivery!