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

Implementing SignalR

parent 0c8e107a
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using RestSharp;
using Stratis.Guru.Hubs;
namespace Stratis.Guru.Services
{
public class TickerService : IHostedService, IDisposable
{
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;
_hub = hub;
_hubContext = hubContext;
}
public Task StartAsync(CancellationToken cancellationToken)
......@@ -22,13 +28,14 @@ namespace Stratis.Guru.Services
var updateTimer = new System.Timers.Timer();
updateTimer.Interval = 10;
updateTimer.Enabled = true;
updateTimer.Elapsed += (sender, args) =>
updateTimer.Elapsed += async (sender, args) =>
{
var coinmarketCapApiClient = new RestClient("https://api.coinmarketcap.com/v2/ticker/1343/");
var coinmarketCapApiRequest = new RestRequest(Method.GET);
var coinmarketcapApi = coinmarketCapApiClient.Execute(coinmarketCapApiRequest);
_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();
return Task.CompletedTask;
......
......@@ -43,6 +43,7 @@ namespace Stratis.Guru
services.AddMemoryCache();
services.AddTransient<UpdateHub>();
services.AddTransient<IAsk, Ask>();
services.AddHostedService<TickerService>();
......
......@@ -13,8 +13,8 @@
<div class="d-inline-block align-middle">
<span class="click-edit">1 STRAT</span> =
</div>
<span class="align-middle display-1 font-weight-bold">@(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 class="align-middle display-1 font-weight-bold" id="amount">@(Model.UsdPrice.ToString("C2"))</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>
<a asp-controller="Home" asp-action="Generator" class="btn-secondary-box"><i class="fa fa-paper-plane"></i> Get a Stratis Address</a>
</div>
......@@ -24,14 +24,15 @@
</section>
@section Scripts
{
<script src="~/npm/@@aspnet/signalr/dist/browser/signalr.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var stratisPrice = @Model.UsdPrice;
var signalr = new signalR.HubConnectionBuilder().withUrl("/update").build();
signalr.on("ReceiveMessage", function (user, message) {
alert("ok");
signalr.on("UpdateTicker", function (message) {
});
signalr.start();
......
......@@ -13,6 +13,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@aspnet/signalr": "^1.0.4",
"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