static void Main(string[] args) { int port = 5000; var server = new HttpServer(port); server.AddJsonRpcService(() => new ChatJsonRpcWebSocketService()); var info = new JsonRpcInfo { Description = "Api for JsonRpc chat", Title = "Chat API", Version = "v1", Contact = new JsonRpcContact { Name = "JsonRpcNet", Email = "*****@*****.**", Url = "https://github.com/JsonRpcNet" } }; server.UseJsonRpcApi(info); server.Start(); Console.WriteLine($"Now listening on: http://localhost:{port}{info.JsonRpcApiEndpoint}"); Console.ReadLine(); server.Stop(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors("AllowAll"); var info = new JsonRpcInfo { Description = "Api for JsonRpc chat", Title = "Chat API", Version = "v1", Contact = new JsonRpcContact { Name = "The Dude", Email = "*****@*****.**", Url = "http://www.thedude.com" } }; app.UseJsonRpcApi(info); app.UseWebSockets(); app.AddJsonRpcService <ChatJsonRpcWebSocketService>(); var doc = new JsonRpcDoc(info); var serviceDoc = DocGenerator.GenerateJsonRpcServiceDoc(typeof(ChatJsonRpcWebSocketService), doc); app.Run(async(context) => { await context.Response.WriteAsync("Hello World!"); }); }
public void GenerateJsonRpcDoc_Valid_CreatesDoc() { // ARRANGE var info = new JsonRpcInfo { Contact = new JsonRpcContact { Email = "*****@*****.**", Name = "test", Url = "www.test.dk" }, Description = "test", Title = "test", Version = "test", JsonRpcApiEndpoint = "/test" }; var attribute = typeof(TestWebSocketService) .GetAttribute <JsonRpcServiceAttribute>(); // ACT var doc = DocGenerator.GenerateJsonRpcDoc(info); // ASSERT Assert.That(doc.Info.Description, Is.EqualTo(info.Description)); Assert.That(doc.Info.Title, Is.EqualTo(info.Title)); Assert.That(doc.Info.Version, Is.EqualTo(info.Version)); Assert.That(doc.Info.JsonRpcApiEndpoint, Is.EqualTo(info.JsonRpcApiEndpoint)); Assert.That(doc.Info.Contact.Email, Is.EqualTo(info.Contact.Email)); Assert.That(doc.Info.Contact.Name, Is.EqualTo(info.Contact.Name)); Assert.That(doc.Info.Contact.Url, Is.EqualTo(info.Contact.Url)); Assert.That(doc.Services, Has.Count.EqualTo(1)); var service = doc.Services.First(); Assert.That(service.Description, Is.EqualTo(attribute.Description)); Assert.That(service.Name, Is.EqualTo(attribute.Name)); Assert.That(service.Path, Is.EqualTo(attribute.Path)); Assert.That(service.Notifications, Has.Count.EqualTo(1)); var notification = service.Notifications.First(); Assert.That(notification.Name, Is.EqualTo("EventWithArgs")); Assert.That(notification.Description, Is.EqualTo("test")); Assert.That(notification.Parameters, Has.Count.EqualTo(1)); var parameters = notification.Parameters.First(); Assert.That(parameters.Name, Is.EqualTo(typeof(TestEventArgs).Name)); Assert.That(parameters.Required, Is.True); Assert.That(parameters.Type, Is.EqualTo(typeof(TestEventArgs))); var schema = parameters.Schema; Assert.That(schema, Contains.Key("type")); Assert.That(schema["type"], Is.EqualTo("object")); Assert.That(schema, Contains.Key("$ref")); Assert.That(schema["$ref"], Is.EqualTo("#/definitions/TestEventArgs")); }
public static void UseJsonRpcApi(this HttpServer server, JsonRpcInfo jsonRpcInfo) { server.OnGet += (s, e) => { var referer = e.Request.UrlReferrer?.AbsolutePath ?? ""; var requestPath = referer + e.Request.Url.AbsolutePath; if (requestPath.StartsWith("//")) { requestPath = requestPath.Substring(1); } var file = JsonRpcFileReader.GetFile(requestPath, jsonRpcInfo); if (!file.Exist) { return; } e.Response.ContentType = MimeTypeProvider.Get(file.Extension); e.Response.ContentEncoding = Encoding.UTF8; // allow cors e.Response.Headers.Add("Access-Control-Allow-Origin", "*"); e.Response.StatusCode = 200; e.Response.WriteContent(file.Buffer); }; }
public static IApplicationBuilder UseJsonRpcApi(this IApplicationBuilder app, JsonRpcInfo jsonRpcInfo) { app.Use(async(context, next) => { var referer = SanitizeReferer(context.Request.GetTypedHeaders().Referer?.AbsolutePath ?? ""); var requestPath = referer + context.Request.Path; var file = JsonRpcFileReader.GetFile(requestPath, jsonRpcInfo); if (!file.Exist) { await next.Invoke(); return; } context.Response.ContentType = MimeTypeProvider.Get(file.Extension); context.Response.StatusCode = 200; await context.Response.Body.WriteAsync(file.Buffer, 0, file.Buffer.Length); }); return(app); }