Skip to content

Instantly share code, notes, and snippets.

@pinhopro
Created July 28, 2016 17:49
Show Gist options
  • Save pinhopro/c34ed8832de1bf9f2a83bfc4632b3248 to your computer and use it in GitHub Desktop.
Save pinhopro/c34ed8832de1bf9f2a83bfc4632b3248 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq; // install Json.NET from 'Manage Nuget Packages...' menu
namespace blinktrade_websocket_client
{
class Program
{
private static async Task BlinkTradeWebSocketTest()
{
using (ClientWebSocket ws = new ClientWebSocket())
{
// connect to blinktrade gateway
Console.WriteLine("Waiting connection...");
Uri serverUri = new Uri("wss://api.testnet.blinktrade.com/trade/");
await ws.ConnectAsync(serverUri, CancellationToken.None);
// Build the json Login Request Message
int reqid = 1;
JObject json_login_request = new JObject();
json_login_request["MsgType"] = "BE";
json_login_request["UserReqID"] = reqid;
json_login_request["Username"] = "user";
json_login_request["Password"] = "abc12345";
json_login_request["UserReqTyp"] = "1";
json_login_request["BrokerID"] = 5;
json_login_request["UserAgent"] = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36";
json_login_request["UserAgentLanguage"] = "en-US";
json_login_request["UserAgentTimezoneOffset"] = ":180,";
json_login_request["UserAgentPlatform"] = "Linux x86_64";
json_login_request["FingerPrint"] = "264206477";
JObject json_stuntip = new JObject();
json_stuntip["local"] = "192.168.0.12;";
json_stuntip["public"] = new JArray("177.35.172.183");
json_login_request["STUNTIP"] = json_stuntip;
// Send Login Request Message on wire AS String
string login_request_message = json_login_request.ToString();
ArraySegment<byte> bytesToSend = new ArraySegment<byte>(Encoding.UTF8.GetBytes(login_request_message));
Console.WriteLine("Sending login request...");
Console.WriteLine("[-->]: " + login_request_message);
await ws.SendAsync(
bytesToSend,
WebSocketMessageType.Text,
true,
CancellationToken.None);
while (ws.State == WebSocketState.Open)
{
// Receive the response
ArraySegment<byte> bytesReceived = new ArraySegment<byte>(new byte[4096]);
WebSocketReceiveResult result = await ws.ReceiveAsync(bytesReceived, CancellationToken.None);
Console.WriteLine("[<--]: " + Encoding.UTF8.GetString(bytesReceived.Array, 0, result.Count));
// build the Test Request using string manipulation instead of a Json Library
string test_request_msg = "{\"MsgType\":\"1\", \"TestReqID\":" + reqid.ToString() +"}";
Console.WriteLine("[-->]: " + test_request_msg);
// Send Test Request
bytesToSend = new ArraySegment<byte>(Encoding.UTF8.GetBytes(test_request_msg));
await ws.SendAsync( bytesToSend, WebSocketMessageType.Text, true, CancellationToken.None);
// Increment the request id
++reqid;
}
}
}
static void Main(string[] args)
{
Task t = BlinkTradeWebSocketTest();
t.Wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment