This post assumes that you've the .NET Framework 4.5 and a C# IDE such as Visual Studio installed locally. If you don't have Visual Studio, you may want to consider downloading and installing the free Visual Studio Express 2013 for Windows Desktop. Once you've determined that you have everything you need, please follow the steps below.
- Create a .NET Console Application project in the C# IDE of your choice and call it "SenseConsoleApp".
- In the newly created project, create a C# class file labelled "SenseWebSocketClient.cs" and enter the following code:
- In "Program.cs", delete all contents and enter the following:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.WebSockets; using System.Threading; namespace SenseConsoleApp { class Program { static void Main(string[] args) { GetDocList(); Console.ReadLine(); } static async Task<string> GetDocList() { var client = new SenseWebSocketClient(new Uri("ws://localhost:4848")); Console.WriteLine("Connecting to Qlik Sense..."); Console.WriteLine("Getting document list..."); var docs = await client.GetDocList(); Console.WriteLine(docs); return docs; } } }
- Launch Qlik Sense Desktop so the local server process runs and can be queried by our console application.
- Now press F5 to run the application. After a short pause, you should see the list of your Qlik Sense Desktop documents in a JSON format.
Console application listing the Qlik Sense documents
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SenseConsoleApp
{
public class SenseWebSocketClient
{
private ClientWebSocket _client;
public Uri _senseServerURI;
public SenseWebSocketClient(Uri senseServerURI)
{
_client = new ClientWebSocket();
_senseServerURI = senseServerURI;
}
public async Task<string> GetDocList()
{
string cmd = "{\"method\":\"GetDocList\",\"handle\":-1,\"params\":[],\"id\":7,\"jsonrpc\":\"2.0\"}";
await _client.ConnectAsync(_senseServerURI, CancellationToken.None);
await SendCommand(cmd);
var docList = await Receive();
return docList;
}
private async Task ConnectToSenseServer()
{
await _client.ConnectAsync(_senseServerURI, CancellationToken.None);
}
private async Task SendCommand(string jsonCmd)
{
ArraySegment<byte> outputBuffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(jsonCmd));
await _client.SendAsync(outputBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
private async Task<string> Receive()
{
var receiveBufferSize = 1536;
byte[] buffer = new byte[receiveBufferSize];
var result = await _client.ReceiveAsync (new ArraySegment<byte>(buffer), CancellationToken.None);
var resultJson = (new UTF8Encoding()).GetString(buffer);
return resultJson;
}
}
}
To summarise, we've sent a JSON command to the Qlik Sense Desktop's local server over the websocket protocol and retrieved the list of documents in a simple console application. The GetDocList JSON command we've sent is as follows:
{"method":"GetDocList","handle":-1,"params":[],"id":7,"jsonrpc":"2.0"}, "requestPartCount": "1"}
For other command examples I recommend that you use Fiddler to monitor the websocket traffic between the Qlik Sense Desktop client and the local Qlik Sense server process as described in this previous post and look at the JSON payload in the client requests. As for the technology you use to implement your custom Qlik Sense client, all you need is a websocket library and a JSON library. My background is in C# which is why I opted for .NET but you should also be able to do this in Java, Python and of course HTML5 to mention a few.I hope you've found this stimulating. Thank you for reading.
No comments:
Post a Comment