Skip to content

Instantly share code, notes, and snippets.

@RoyiNamir
Created January 12, 2018 12:32
Show Gist options
  • Save RoyiNamir/f61518550b3f83c859b3b3350605b61a to your computer and use it in GitHub Desktop.
Save RoyiNamir/f61518550b3f83c859b3b3350605b61a to your computer and use it in GitHub Desktop.
gist
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using StackExchange.Redis;
namespace WebApplication2.Controllers
{
public class Controller1Controller : ApiController
{
readonly ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
[HttpGet]
public async Task<HttpResponseMessage> Login(string loginName)
{
var result = await GoToRedis(loginName); //pause the request until an appropriate response
return Request.CreateResponse(HttpStatusCode.OK, result); ;
}
public async Task<Task<bool>> GoToRedis(string loginName) //redis connects the LOGIN interface AND a far distance server
//which actually *has the knowledge* to check if login is valid or not.
{
var tcs = new TaskCompletionSource<bool>();
ISubscriber sub = redis.GetSubscriber();
await sub.SubscribeAsync("channel", (channel, message) =>
{
//wait for far server publish
if (message == loginName + "OK")
tcs.SetResult(true);
});
return tcs.Task;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment