Navigation Menu

Skip to content

Commit

Permalink
Add example of native client
Browse files Browse the repository at this point in the history
  • Loading branch information
robzhu committed May 11, 2020
1 parent eee7da5 commit 74e0018
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
38 changes: 37 additions & 1 deletion .gitignore
Expand Up @@ -113,4 +113,40 @@ dist
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.pnp.*
.pnp.*

# dotnet
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode

# Rider
.idea

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn
52 changes: 52 additions & 0 deletions dotnet-client/Program.cs
@@ -0,0 +1,52 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Websocket.Client;

namespace dotnet_client
{
static class Program
{
const string LoginEndpoint = "http://localhost:9000/login";
const string UserID = "1234";
static Uri WebSocketEndpoint = new Uri("ws://localhost:9001");

static async Task Main(string[] args)
{
HttpClient client = new HttpClient();

client.DefaultRequestHeaders.Add("user", UserID);
dynamic response = JsonConvert.DeserializeObject(await client.GetStringAsync(LoginEndpoint));
string sessionId = response.sessionId;
Console.WriteLine("Obtained session ID: " + sessionId);

using (var socket = new WebsocketClient(WebSocketEndpoint))
{
await socket.Start();

socket.MessageReceived.Subscribe(msg =>
{
dynamic payload = JsonConvert.DeserializeObject(msg.Text);
if (payload["event"] == "sessionInvalidated")
{
Console.WriteLine("You have logged in elsewhere. Exiting.");
Environment.Exit(0);
}
});

socket.Send(JsonConvert.SerializeObject(new
{
action = "subscribeToSessionInvalidation",
args = new
{
sessionId = sessionId
}
}));

Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
}
}
}
14 changes: 14 additions & 0 deletions dotnet-client/dotnet-client.csproj
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>dotnet_client</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Websocket.Client" Version="4.3.12" />
</ItemGroup>

</Project>

0 comments on commit 74e0018

Please sign in to comment.