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.
Service.ConnectionCreated += new ConnectionCreatedEventHandler(kwwika_ConnectionCreated);
string apiKey = "myAPIKey";
string domain = "www.example.com";
Service.Connect(apiKey, domain);
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());
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());