Created
January 12, 2018 13:00
-
-
Save RoyiNamir/eaaf80c8104232d103d6e5a4c3f60d06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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