Commit 9ace9991 authored by Clint Mourlevat's avatar Clint Mourlevat

Implementing SignalR

parent 0c8e107a
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Newtonsoft.Json; using Newtonsoft.Json;
using RestSharp; using RestSharp;
using Stratis.Guru.Hubs;
namespace Stratis.Guru.Services namespace Stratis.Guru.Services
{ {
public class TickerService : IHostedService, IDisposable public class TickerService : IHostedService, IDisposable
{ {
private readonly IMemoryCache _memoryCache; private readonly IMemoryCache _memoryCache;
private readonly IHubContext<UpdateHub> _hubContext;
private readonly UpdateHub _hub;
public TickerService(IMemoryCache memoryCache) public TickerService(IMemoryCache memoryCache, UpdateHub hub, IHubContext<UpdateHub> hubContext)
{ {
_memoryCache = memoryCache; _memoryCache = memoryCache;
_hub = hub;
_hubContext = hubContext;
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
...@@ -22,13 +28,14 @@ namespace Stratis.Guru.Services ...@@ -22,13 +28,14 @@ namespace Stratis.Guru.Services
var updateTimer = new System.Timers.Timer(); var updateTimer = new System.Timers.Timer();
updateTimer.Interval = 10; updateTimer.Interval = 10;
updateTimer.Enabled = true; updateTimer.Enabled = true;
updateTimer.Elapsed += (sender, args) => updateTimer.Elapsed += async (sender, args) =>
{ {
var coinmarketCapApiClient = new RestClient("https://api.coinmarketcap.com/v2/ticker/1343/"); var coinmarketCapApiClient = new RestClient("https://api.coinmarketcap.com/v2/ticker/1343/");
var coinmarketCapApiRequest = new RestRequest(Method.GET); var coinmarketCapApiRequest = new RestRequest(Method.GET);
var coinmarketcapApi = coinmarketCapApiClient.Execute(coinmarketCapApiRequest); var coinmarketcapApi = coinmarketCapApiClient.Execute(coinmarketCapApiRequest);
_memoryCache.Set("Coinmarketcap", coinmarketcapApi.Content); _memoryCache.Set("Coinmarketcap", coinmarketcapApi.Content);
updateTimer.Interval = TimeSpan.FromMinutes(10).TotalMilliseconds; updateTimer.Interval = TimeSpan.FromSeconds(10).TotalMilliseconds;
await _hubContext.Clients.All.SendAsync("UpdateTicker", cancellationToken);
}; };
updateTimer.Start(); updateTimer.Start();
return Task.CompletedTask; return Task.CompletedTask;
......
...@@ -43,6 +43,7 @@ namespace Stratis.Guru ...@@ -43,6 +43,7 @@ namespace Stratis.Guru
services.AddMemoryCache(); services.AddMemoryCache();
services.AddTransient<UpdateHub>();
services.AddTransient<IAsk, Ask>(); services.AddTransient<IAsk, Ask>();
services.AddHostedService<TickerService>(); services.AddHostedService<TickerService>();
......
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
<div class="d-inline-block align-middle"> <div class="d-inline-block align-middle">
<span class="click-edit">1 STRAT</span> = <span class="click-edit">1 STRAT</span> =
</div> </div>
<span class="align-middle display-1 font-weight-bold">@(Model.UsdPrice.ToString("C2"))</span> <span class="align-middle display-1 font-weight-bold" id="amount">@(Model.UsdPrice.ToString("C2"))</span>
<span class="text-@(Model.Last24Change > 0 ? "success":"danger") font-weight-bold"><sup> @((Model.Last24Change > 0 ? "+":"-")) @(Model.Last24Change.ToString("P2"))</sup></span> <span id="lastchange" class="text-@(Model.Last24Change > 0 ? "success":"danger") font-weight-bold"><sup> @((Model.Last24Change > 0 ? "+":"-")) @(Model.Last24Change.ToString("P2"))</sup></span>
</h1> </h1>
<a asp-controller="Home" asp-action="Generator" class="btn-secondary-box"><i class="fa fa-paper-plane"></i> Get a Stratis Address</a> <a asp-controller="Home" asp-action="Generator" class="btn-secondary-box"><i class="fa fa-paper-plane"></i> Get a Stratis Address</a>
</div> </div>
...@@ -24,14 +24,15 @@ ...@@ -24,14 +24,15 @@
</section> </section>
@section Scripts @section Scripts
{ {
<script src="~/npm/@@aspnet/signalr/dist/browser/signalr.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
var stratisPrice = @Model.UsdPrice; var stratisPrice = @Model.UsdPrice;
var signalr = new signalR.HubConnectionBuilder().withUrl("/update").build(); var signalr = new signalR.HubConnectionBuilder().withUrl("/update").build();
signalr.on("ReceiveMessage", function (user, message) { signalr.on("UpdateTicker", function (message) {
alert("ok");
}); });
signalr.start(); signalr.start();
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@aspnet/signalr": "^1.0.4",
"bootstrap": "^4.1.3" "bootstrap": "^4.1.3"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment