| WebSync On-Demand Tutorials | WebSync Server Tutorials |
|---|---|
|
|
|
In this tutorial you will learn how to attach custom code to WebSync Server events. We will use a simple publish hook to modify messages on-the-fly.
Before you can start coding, you need to have the correct project references.
Methods are attached to WebSync events by adding a simple attribute to the method definition.
We create a generic class to hold the method, define a Data
class for the purposes of deserialization, and create a method that will
attach to the BeforePublish event.
// a generic class that contains our WebSync events
public class WebSyncEvents
{
[DataContract]
private class Data
{
[DataMember(Name = "text")]
public string Text { get; set; }
}
[WebSyncEvent(EventType.BeforePublish)]
public static void UpperCasePublishes(object sender, WebSyncEventArgs e)
{
foreach (Message message in e.Messages)
{
Data data = JSON.Deserialize<Data>(message.DataJson);
data.Text = data.Text.ToUpperInvariant();
message.DataJson = JSON.Serialize(data);
}
}
}
Open up your client page in a couple windows and watch as the publications are upper-cased before delivery!