| WebSync On-Demand Tutorials | WebSync Server Tutorials |
|---|---|
|
|
|
Want more control over your messages? In this tutorial, you will learn how our pre-built proxies can be used to intercept messages, read them, and change them. The sky is the limit with what can be done!
Before you can start coding, you need to have the correct project references.
Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyProxy.aspx.cs" Inherits="Tutorials.MyProxy" %>
Code-Behind:
namespace Tutorials
{
public partial class MyProxy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Proxy.Invoke(new ProxyInvokeArgs()
{
DomainKey = "22222222-2222-2222-2222-222222222222", // your *private* key
OnBeforeProxy = (args) =>
{
// handle the requests here
}
});
}
}
}
Right now, the proxy just relays requests without modifications. We'll make it more interesting in a moment.
The JavaScript client has to be configured to target the proxy. In this example, we're interested in proxying publications.
Modify the HTML page created in the WebSync On-Demand: Basic tutorial
by adding requestUrl.
client.publish({
requestUrl: '/myproxy.aspx',
...
});
Yes, it's that easy. Now we have to make the proxy do something useful.
In this example, we're going to take the incoming text and make it upper-case.
We define a Data class for the purpose of deserialization
and in the proxy callback, iterate over the messages and modify the Data property.
[DataContract]
private class Data
{
[DataMember(Name = "text")]
public string Text { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
Proxy.Invoke(new ProxyInvokeArgs()
{
DomainKey = "11111111-1111-1111-1111-111111111111", // replace with your private key
OnBeforeProxy = (args) =>
{
foreach (Message message in args.Messages)
{
// handle the messages here
if (message.IsPublish())
{
// deserialize, modify, then reserialize the data
Data data = JSON.Deserialize<Data>(message.DataJson);
data.Text = data.Text.ToUpperInvariant();
message.DataJson = JSON.Serialize(data);
}
}
}
});
}
Open the page in a couple browser windows. Watch as the publications are upper-cased before delivery!