Skip to content

Instantly share code, notes, and snippets.

@RoyiNamir
Created January 12, 2018 13:00
Show Gist options
  • Save RoyiNamir/eaaf80c8104232d103d6e5a4c3f60d06 to your computer and use it in GitHub Desktop.
Save RoyiNamir/eaaf80c8104232d103d6e5a4c3f60d06 to your computer and use it in GitHub Desktop.
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