WebSync Tutorials - Frozen Mountain
Note: these tuturials are for WebSync 2 only.

If you are using WebSync 3, you want our WebSync 3 tutorials.

WebSync 2 Tutorials




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.
  1. Add a reference to FM.WebSync.Core (available as part of the WebSync Server downloads).
  2. Add a reference to FM.WebSync.Server (available as part of the WebSync Server downloads).
  3. 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.
  1. Modify the request handler created in the WebSync Server: Basic tutorial.
    1. Define a Data class for the purpose of deserialization.
    2. Add a method called PrePublish.
    3. 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!