Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Commit 379f238

Browse files
committed
Adding StockTickR App
0 parents  commit 379f238

22 files changed

+1142
-0
lines changed

.gitattributes

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
*.doc diff=astextplain
2+
*.DOC diff=astextplain
3+
*.docx diff=astextplain
4+
*.DOCX diff=astextplain
5+
*.dot diff=astextplain
6+
*.DOT diff=astextplain
7+
*.pdf diff=astextplain
8+
*.PDF diff=astextplain
9+
*.rtf diff=astextplain
10+
*.RTF diff=astextplain
11+
12+
*.jpg binary
13+
*.png binary
14+
*.gif binary
15+
16+
*.cs text=auto diff=csharp
17+
*.vb text=auto
18+
*.resx text=auto
19+
*.c text=auto
20+
*.cpp text=auto
21+
*.cxx text=auto
22+
*.h text=auto
23+
*.hxx text=auto
24+
*.py text=auto
25+
*.rb text=auto
26+
*.java text=auto
27+
*.html text=auto
28+
*.htm text=auto
29+
*.css text=auto
30+
*.scss text=auto
31+
*.sass text=auto
32+
*.less text=auto
33+
*.js text=auto
34+
*.lisp text=auto
35+
*.clj text=auto
36+
*.sql text=auto
37+
*.php text=auto
38+
*.lua text=auto
39+
*.m text=auto
40+
*.asm text=auto
41+
*.erl text=auto
42+
*.fs text=auto
43+
*.fsx text=auto
44+
*.hs text=auto
45+
46+
*.csproj text=auto
47+
*.vbproj text=auto
48+
*.fsproj text=auto
49+
*.dbproj text=auto
50+
*.sln text=auto eol=crlf
51+
52+
*.sh eol=lf

.gitignore

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[Oo]bj/
2+
[Bb]in/
3+
TestResults/
4+
.nuget/
5+
*.sln.ide/
6+
_ReSharper.*/
7+
packages/
8+
artifacts/
9+
PublishProfiles/
10+
.vs/
11+
*.user
12+
*.suo
13+
*.cache
14+
*.docstates
15+
_ReSharper.*
16+
nuget.exe
17+
*net45.csproj
18+
*net451.csproj
19+
*k10.csproj
20+
*.psess
21+
*.vsp
22+
*.pidb
23+
*.userprefs
24+
*DS_Store
25+
*.ncrunchsolution
26+
*.*sdf
27+
*.ipch
28+
project.lock.json
29+
runtimes/
30+
.build/
31+
.testPublish/
32+
launchSettings.json
33+
node_modules/
34+
npm-debug.log
35+
*.tmp
36+
*.nuget.props
37+
*.nuget.targets
38+
autobahnreports/
39+
site.min.css
40+
.idea/
41+
.vscode/
42+
signalr-client/
43+
dist/
44+
global.json
45+
BenchmarkDotNet.Artifacts/
46+
korebuild-lock.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
10+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.0.0-alpha1-final" />
11+
</ItemGroup>
12+
13+
</Project>

StockTickR/CsharpClient/Program.cs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.SignalR.Client;
6+
using Microsoft.AspNetCore.Sockets;
7+
8+
namespace CsharpClient
9+
{
10+
public class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
Task.Run(Run).Wait();
15+
}
16+
17+
static async Task Run()
18+
{
19+
var connection = new HubConnectionBuilder()
20+
.WithUrl("http://localhost:5000/signalr")
21+
.WithConsoleLogger()
22+
.WithMessagePackProtocol()
23+
.WithTransport(TransportType.WebSockets)
24+
.Build();
25+
26+
await connection.StartAsync();
27+
28+
Console.WriteLine("Starting connection. Press Ctrl-C to close.");
29+
var cts = new CancellationTokenSource();
30+
Console.CancelKeyPress += (sender, a) =>
31+
{
32+
a.Cancel = true;
33+
cts.Cancel();
34+
};
35+
36+
connection.Closed += e =>
37+
{
38+
Console.WriteLine("Connection closed with error: {0}", e);
39+
40+
cts.Cancel();
41+
return Task.CompletedTask;
42+
};
43+
44+
connection.On("marketOpened", async () =>
45+
{
46+
await StartStreaming();
47+
});
48+
49+
// Do an initial check to see if we can start streaming the stocks
50+
var state = await connection.InvokeAsync<string>("GetMarketState");
51+
if (string.Equals(state, "Open"))
52+
{
53+
await StartStreaming();
54+
}
55+
56+
// Keep client running until cancel requested.
57+
while (!cts.IsCancellationRequested)
58+
{
59+
await Task.Delay(250);
60+
}
61+
62+
async Task StartStreaming()
63+
{
64+
var channel = connection.Stream<Stock>("StreamStocks", CancellationToken.None);
65+
while (await channel.WaitToReadAsync() && !cts.IsCancellationRequested)
66+
{
67+
while (channel.TryRead(out var stock))
68+
{
69+
Console.WriteLine($"{stock.Symbol} {stock.Price}");
70+
}
71+
}
72+
}
73+
}
74+
}
75+
76+
public class Stock
77+
{
78+
public string Symbol { get; set; }
79+
80+
public decimal DayOpen { get; private set; }
81+
82+
public decimal DayLow { get; private set; }
83+
84+
public decimal DayHigh { get; private set; }
85+
86+
public decimal LastChange { get; private set; }
87+
88+
public decimal Change { get; set; }
89+
90+
public double PercentChange { get; set; }
91+
92+
public decimal Price { get; set; }
93+
}
94+
}

StockTickR/NodeClient/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@aspnet:registry=https://www.myget.org/f/signalr-alpha/npm/

StockTickR/NodeClient/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## How to use
2+
Launch server application first.
3+
```
4+
$ npm install
5+
$ tsc
6+
$ node src/main.js
7+
```

StockTickR/NodeClient/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"dependencies": {
3+
"websocket": "^1.0.24",
4+
"xmlhttprequest": "^1.8.0",
5+
"msgpack5": "^3.5.1",
6+
"eventsource": "^1.0.5",
7+
"atob": "^2.0.3",
8+
"btoa": "^1.1.2"
9+
},
10+
"files": [
11+
"src/**/*"
12+
],
13+
"devDependencies": {
14+
"@aspnet/signalr-client": "~1.0.0-alpha1",
15+
"@types/node": "^8.0.28"
16+
}
17+
}

StockTickR/NodeClient/src/main.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const signalR = require("@aspnet/signalr-client");
4+
const EventSource = require("eventsource");
5+
global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
6+
global.WebSocket = require("websocket").w3cwebsocket;
7+
global.EventSource = EventSource;
8+
global.btoa = require("btoa");
9+
global.atob = require("atob");
10+
const hubConnection = new signalR.HubConnection("http://localhost:5000/signalr");
11+
hubConnection.start().then(() => {
12+
hubConnection.invoke("GetMarketState").then(function (state) {
13+
if (state === "Open") {
14+
streamStocks();
15+
}
16+
else {
17+
hubConnection.invoke("OpenMarket");
18+
}
19+
});
20+
}).catch(err => {
21+
console.log(err);
22+
});
23+
hubConnection.on("marketOpened", function () {
24+
streamStocks();
25+
});
26+
function streamStocks() {
27+
hubConnection.stream("StreamStocks").subscribe({
28+
next: (stock) => {
29+
console.log(stock);
30+
// console.log(stock.Symbol + " " + stock.Price);
31+
},
32+
error: () => { },
33+
complete: () => { }
34+
});
35+
}

StockTickR/NodeClient/src/main.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as signalR from "@aspnet/signalr-client";
2+
import * as EventSource from "eventsource";
3+
4+
(<any>global).XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
5+
(<any>global).WebSocket = require("websocket").w3cwebsocket;
6+
(<any>global).EventSource = EventSource;
7+
(<any>global).btoa = require("btoa");
8+
(<any>global).atob = require("atob");
9+
10+
const hubConnection : signalR.HubConnection = new signalR.HubConnection("http://localhost:5000/signalr");
11+
12+
hubConnection.start().then(() => {
13+
hubConnection.invoke("GetMarketState").then(function (state : string): void {
14+
if (state === "Open") {
15+
streamStocks();
16+
} else {
17+
hubConnection.invoke("OpenMarket");
18+
}
19+
});
20+
}).catch(err => {
21+
console.log(err);
22+
});
23+
24+
hubConnection.on("marketOpened", function(): void {
25+
streamStocks();
26+
});
27+
28+
function streamStocks (): void {
29+
hubConnection.stream("StreamStocks").subscribe({
30+
next: (stock : any) => {
31+
console.log(stock);
32+
// console.log(stock.Symbol + " " + stock.Price);
33+
},
34+
error: () => {},
35+
complete: () => {}
36+
});
37+
}

StockTickR/NodeClient/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs"
5+
},
6+
"include": [
7+
"src/main.ts"
8+
],
9+
"paths": {
10+
"*": [
11+
"node_modules/@aspnet/signalr-client/dist/src/*"
12+
]
13+
}
14+
}

StockTickR/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## StockTickR Server
2+
The server application is located in the `StockTickRApp` folder.
3+
4+
To run:
5+
```
6+
$ dotnet restore
7+
$ dotnet run
8+
```
9+
10+
Application is hosted on `http://localhost:5000` by default.
11+
12+
## Node Client
13+
The node client is located in the `nodeClient` folder.
14+
Instructions are in the README there.

StockTickR/StockTickRApp/Program.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace SignalR.StockTicker
9+
{
10+
public class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
var host = new WebHostBuilder()
15+
.UseSetting(WebHostDefaults.PreventHostingStartupKey, "true")
16+
.ConfigureLogging((context, factory) =>
17+
{
18+
factory.AddConfiguration(context.Configuration.GetSection("Logging"));
19+
factory.AddConsole();
20+
factory.AddDebug();
21+
})
22+
.UseKestrel()
23+
.UseContentRoot(Directory.GetCurrentDirectory())
24+
.UseIISIntegration()
25+
.UseStartup<Startup>()
26+
.Build();
27+
28+
host.Run();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)