Kwwika ceased operation on 01/06/2011. Please migrate to Pusher

API‎ > ‎

Silverlight

Library and Examples

You can download the Silverlight library and an example chat project on github.

Connect

Connecting using the Kwwika Silverlight API is slightly different to the other APIs. You need to connect in an asynchronous way by first register to a ConnectionCreated event and then calling a factory method to create the connection to the Kwwika service.

IConnection connection;

public MainPage()
{
    InitializeComponent(); // Silverlight Framework Page call          

    Service.ConnectionCreated += new ConnectionCreatedEventHandler(kwwika_ConnectionCreated);
    string apiKey = "myAPIKey";
    string domain = "www.example.com";
    Service.Connect(apiKey, domain);
}

void kwwika_ConnectionCreated(ConnectionCreatedEventArgs e)
{
    // Get a reference to the connection to Kwwika
    connection = e.Connection;

    // Connected so we can now subscribe and publish
}

Disconnect

When your application has finished using the service, for example the users is closing the application or web page you should disconnect from Kwwika. This can be acheived with a simple call to Disconnect() on the IConnection interface.

connection.Disconnect();

Subscribe

Onces you have connected to Kwwika you can subscribe to data. We use something called a topic to identify information. For instance our example chat application uses the /KWWIKA/EXAMPLES/CHAT topic. When you subscribe to a topic you also pass an object that defines two methods.topicUpdated is called when information becomes available for the topic you've subscribed to, and topicError is called if there is a problem with your subscription such as the topic does not exist within Kwwika.

// define a subscription listener
public class MySubscriptionListener: ISubscriptionListener
{
    public void TopicError(ISubscription sub, CommandErrorType error)
    {
        Debug.WriteLine(error.ToString());
    }

    public void TopicUpdated(ISubscription sub, Dictionary<string, string> values, bool isImage)
    {
        Debug.WriteLine("Topic Updated: " + sub.TopicName);
    }
}

// subscribe
connection.Subscribe("/KWWIKA/EXAMPLES/CHAT", new MySubscriptionListener());

Publish

We've tried to keep publishing data as simple as possible. All you need to do is specify the topic you want to publish to, the names and values that you want to publish, and provide an object that will be informed about whether your attempt to publish succeeded or not.

// create a command listener
public class MyCommandListener: ICommandListener
{
    public void CommandError(string topic, CommandErrorType code)
    {
        Debug.WriteLine("Publish failed: " + code.ToString());
    }

    public void CommandSuccess(string topic)
    {
        Debug.WriteLine("Publish successful");
    }
}

TimeSpan t = (sendTime - new DateTime(1970, 1, 1));
int millisSinceEpoch = ((int)t.TotalSeconds) * 1000;
var values = new Dictionary<string, string>() { { "name", "Phil"}, { "text", "Hello Kwwika!" }, { "datetime", millisSinceEpoch.ToString() } };
connection.Publish("/KWWIKA/EXAMPLES/CHAT", values, new MyCommandListener());

Sign in  |  Recent Site Activity  |  Terms  |  Report Abuse  |  Print page  |  Powered by Google Sites