private void SendResponseObject(HttpListenerContext context, object responseObject) { if (responseObject != null && responseObject.GetType().Equals(typeof(byte[])) == false) { string responseText = ""; if (responseObject is string) { responseText = responseObject.ToString(); } else { responseText = MigService.JsonSerialize(responseObject); } // simple automatic json response type detection if (responseText.StartsWith("[") && responseText.EndsWith("]") || responseText.StartsWith("{") && responseText.EndsWith("}")) { // send as JSON context.Response.ContentType = "application/json"; context.Response.ContentEncoding = defaultWebFileEncoding; } WebServiceUtility.WriteStringToContext(context, responseText); } else { // Send as binary data WebServiceUtility.WriteBytesToContext(context, (byte[])responseObject); } }
public void OnInterfacePropertyChanged(object sender, InterfacePropertyChangedEventArgs args) { if (webSocketServer != null && webSocketServer.IsListening) { WebSocketServiceHost host; webSocketServer.WebSocketServices.TryGetServiceHost("/events", out host); if (host == null) { return; } host.Sessions.BroadcastAsync(MigService.JsonSerialize(args.EventData), () => {}); } }
public void OnInterfacePropertyChanged(object sender, InterfacePropertyChangedEventArgs args) { wsocketServer.WebSocketServices.Broadcast(MigService.JsonSerialize(args.EventData)); }
public void OnInterfacePropertyChanged(object sender, InterfacePropertyChangedEventArgs args) { UTF8Encoding encoding = new UTF8Encoding(); server.SendAll(encoding.GetBytes(MigService.JsonSerialize(args))); }
private void HandleEventsRoute(HttpListenerRequest request, HttpListenerResponse response, HttpListenerContext context, string remoteAddress) { // Server sent events // NOTE: no PreProcess or PostProcess events are fired in this case //response.KeepAlive = true; response.ContentEncoding = Encoding.UTF8; response.ContentType = "text/event-stream"; response.Headers.Set(HttpResponseHeader.CacheControl, "no-cache, no-store, must-revalidate"); response.Headers.Set(HttpResponseHeader.Pragma, "no-cache"); response.Headers.Set("Access-Control-Allow-Origin", "*"); // 2K padding for IE var padding = ":" + new String(' ', 2048) + "\n"; byte[] paddingData = Encoding.UTF8.GetBytes(padding); response.OutputStream.Write(paddingData, 0, paddingData.Length); byte[] retryData = Encoding.UTF8.GetBytes("retry: 1000\n"); response.OutputStream.Write(retryData, 0, retryData.Length); DateTime lastTimeStamp = DateTime.UtcNow; var lastId = context.Request.Headers.Get("Last-Event-ID"); if (lastId == null || lastId == "") { var queryValues = HttpUtility.ParseQueryString(context.Request.Url.Query); lastId = queryValues.Get("lastEventId"); } if (lastId != null && lastId != "") { double unixTimestamp = 0; double.TryParse(lastId, NumberStyles.Float | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out unixTimestamp); if (unixTimestamp != 0) { lastTimeStamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); lastTimeStamp.AddSeconds(Math.Round(unixTimestamp / 1000d)); } } bool connected = true; var timeoutWatch = Stopwatch.StartNew(); while (connected) { // dirty work around for signaling new event and // avoiding locks on long socket timetout lock (sseEventToken) Monitor.Wait(sseEventToken, 1000); // safely dequeue events List <SseEvent> bufferedData; do { bufferedData = sseEventBuffer.FindAll(le => le != null && le.Timestamp.Ticks > lastTimeStamp.Ticks); if (bufferedData.Count > 0) { foreach (SseEvent entry in bufferedData) { // send events try { // The following throws an error on some mono-arm (Input string was not in the correct format) // entry.Event.UnixTimestamp.ToString("R", CultureInfo.InvariantCulture) byte[] data = Encoding.UTF8.GetBytes("id: " + entry.Event.UnixTimestamp.ToString().Replace(",", ".") + "\ndata: " + MigService.JsonSerialize(entry.Event) + "\n\n"); response.OutputStream.Write(data, 0, data.Length); //response.OutputStream.Flush(); lastTimeStamp = entry.Timestamp; } catch (Exception e) { MigService.Log.Info(new MigEvent(this.GetName(), remoteAddress, "HTTP", request.HttpMethod, $"{response.StatusCode} {request.RawUrl} [ERROR: {e.Message}]")); connected = false; break; } } Thread.Sleep(100); } // there might be new data after sending } while (connected && bufferedData.Count > 0); // check if the remote end point is still alive every 15 seconds or so if (timeoutWatch.Elapsed.TotalSeconds > 15) { connected = connected && IsRemoteEndPointConnected(request.RemoteEndPoint); timeoutWatch.Stop(); timeoutWatch = Stopwatch.StartNew(); } } }
public static void Main(string[] args) { string webPort = "8088"; Console.WriteLine("MigService test APP"); Console.WriteLine("URL: http://localhost:{0}", webPort); var migService = new MigService(); // Add and configure the Web gateway var web = migService.AddGateway("WebServiceGateway"); web.SetOption("HomePath", "html"); web.SetOption("BaseUrl", "/pages/"); web.SetOption("Host", "*"); web.SetOption("Port", webPort); web.SetOption("Password", ""); web.SetOption("EnableFileCaching", "False"); // Add and configure the Web Socket gateway var ws = migService.AddGateway("WebSocketGateway"); ws.SetOption("Port", "8181"); // Configuration can also be loaded from a file as shown below /* * MigServiceConfiguration configuration; * // Construct an instance of the XmlSerializer with the type * // of object that is being deserialized. * XmlSerializer mySerializer = new XmlSerializer(typeof(MigServiceConfiguration)); * // To read the file, create a FileStream. * FileStream myFileStream = new FileStream("systemconfig.xml", FileMode.Open); * // Call the Deserialize method and cast to the object type. * configuration = (MigServiceConfiguration)mySerializer.Deserialize(myFileStream); * // Set the configuration * migService.Configuration = configuration; */ migService.StartService(); // Enable some interfaces for testing... /* * var zwave = migService.AddInterface("HomeAutomation.ZWave", "MIG.HomeAutomation.dll"); * zwave.SetOption("Port", "/dev/ttyUSB0"); * migService.EnableInterface("HomeAutomation.ZWave"); */ /* * var upnp = migService.AddInterface("Protocols.UPnP", "MIG.Protocols.dll"); * migService.EnableInterface("Protocols.UPnP"); */ migService.RegisterApi("myapp/demo", (request) => { Console.WriteLine("Received API call from source {0}\n", request.Context.Source); Console.WriteLine("[Context data]\n{0}\n", MigService.JsonSerialize(request.Context.Data, true)); Console.WriteLine("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true)); var cmd = request.Command; // cmd.Domain is the first element in the API URL (myapp) // cmd.Address is the second element in the API URL (demo) // cmd.Command is the third element in the API URL (greet | echo | ping) // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n) switch (cmd.Command) { case "greet": var name = cmd.GetOption(0); migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Greet", "Greet.User", name); break; case "echo": string fullRequestPath = cmd.OriginalRequest; migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Echo", "Echo.Data", fullRequestPath); break; case "ping": migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Ping", "Ping.Reply", "PONG"); break; } return(new ResponseStatus(Status.Ok)); }); while (true) { Thread.Sleep(10000); } }
private void Worker(object state) { HttpListenerRequest request = null; HttpListenerResponse response = null; try { var context = state as HttpListenerContext; // request = context.Request; response = context.Response; // if (request.UserLanguages != null && request.UserLanguages.Length > 0) { try { CultureInfo culture = CultureInfo.CreateSpecificCulture(request.UserLanguages[0].ToLowerInvariant().Trim()); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; } catch { } } // if (request.IsSecureConnection) { var clientCertificate = request.GetClientCertificate(); X509Chain chain = new X509Chain(); chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; chain.Build(clientCertificate); if (chain.ChainStatus.Length != 0) { // Invalid certificate response.StatusCode = (int)HttpStatusCode.Unauthorized; response.OutputStream.Close(); return; } } // response.Headers.Set(HttpResponseHeader.Server, "MIG WebService Gateway"); response.KeepAlive = false; // bool isAuthenticated = (request.Headers["Authorization"] != null); string remoteAddress = request.RemoteEndPoint.Address.ToString(); string logExtras = ""; // if (servicePassword == "" || isAuthenticated) //request.IsAuthenticated) { bool verified = false; // string authUser = ""; string authPass = ""; // //NOTE: context.User.Identity and request.IsAuthenticated //aren't working under MONO with this code =/ //so we proceed by manually parsing Authorization header // //HttpListenerBasicIdentity identity = null; // if (isAuthenticated) { //identity = (HttpListenerBasicIdentity)context.User.Identity; // authuser = identity.Name; // authpass = identity.Password; byte[] encodedDataAsBytes = System.Convert.FromBase64String(request.Headers["Authorization"].Split(' ')[1]); string authtoken = System.Text.Encoding.UTF8.GetString(encodedDataAsBytes); authUser = authtoken.Split(':')[0]; authPass = authtoken.Split(':')[1]; } // //TODO: complete authorization (for now with one fixed user 'admin', add multiuser support) // if (servicePassword == "" || (authUser == serviceUsername && Utility.Encryption.SHA1.GenerateHashString(authPass) == servicePassword)) { verified = true; } // if (verified) { string url = request.RawUrl.TrimStart('/').TrimStart('\\').TrimStart('.'); if (url.IndexOf("?") > 0) { url = url.Substring(0, url.IndexOf("?")); } // Check if this url is an alias url = UrlAliasCheck(url.TrimEnd('/')); // // url aliasing check if (url == "" || url.TrimEnd('/') == baseUrl.TrimEnd('/')) { // default home redirect response.Redirect("/" + baseUrl.TrimEnd('/') + "/index.html"); //TODO: find a solution for HG homepage redirect ---> ?" + new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds + "#page_control"); response.Close(); } else { var connectionWatch = Stopwatch.StartNew(); MigService.Log.Info(new MigEvent(this.GetName(), remoteAddress, "HTTP", request.HttpMethod.ToString(), String.Format("{0} {1} [OPEN]", response.StatusCode, request.RawUrl))); // this url is reserved for Server Sent Event stream if (url.TrimEnd('/').Equals("events")) { // TODO: move all of this to a separate function // Server sent events // NOTE: no PreProcess or PostProcess events are fired in this case //response.KeepAlive = true; response.ContentEncoding = Encoding.UTF8; response.ContentType = "text/event-stream"; response.Headers.Set(HttpResponseHeader.CacheControl, "no-cache, no-store, must-revalidate"); response.Headers.Set(HttpResponseHeader.Pragma, "no-cache"); response.Headers.Set("Access-Control-Allow-Origin", "*"); // 2K padding for IE var padding = ":" + new String(' ', 2048) + "\n"; byte[] paddingData = System.Text.Encoding.UTF8.GetBytes(padding); response.OutputStream.Write(paddingData, 0, paddingData.Length); byte[] retryData = System.Text.Encoding.UTF8.GetBytes("retry: 1000\n"); response.OutputStream.Write(retryData, 0, retryData.Length); DateTime lastTimeStamp = DateTime.UtcNow; var lastId = context.Request.Headers.Get("Last-Event-ID"); if (lastId == null || lastId == "") { var queryValues = HttpUtility.ParseQueryString(context.Request.Url.Query); lastId = queryValues.Get("lastEventId"); } if (lastId != null && lastId != "") { double unixTimestamp = 0; double.TryParse(lastId, NumberStyles.Float | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out unixTimestamp); if (unixTimestamp != 0) { lastTimeStamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); lastTimeStamp.AddSeconds(Math.Round(unixTimestamp / 1000d)); } } bool connected = true; var timeoutWatch = Stopwatch.StartNew(); while (connected) { // dirty work around for signaling new event and // avoiding locks on long socket timetout lock (sseEventToken) Monitor.Wait(sseEventToken, 1000); // safely dequeue events List <SseEvent> bufferedData; do { bufferedData = sseEventBuffer.FindAll(le => le != null && le.Timestamp.Ticks > lastTimeStamp.Ticks); if (bufferedData.Count > 0) { foreach (SseEvent entry in bufferedData) { // send events try { // The following throws an error on some mono-arm (Input string was not in the correct format) // entry.Event.UnixTimestamp.ToString("R", CultureInfo.InvariantCulture) byte[] data = System.Text.Encoding.UTF8.GetBytes("id: " + entry.Event.UnixTimestamp.ToString().Replace(",", ".") + "\ndata: " + MigService.JsonSerialize(entry.Event) + "\n\n"); response.OutputStream.Write(data, 0, data.Length); //response.OutputStream.Flush(); lastTimeStamp = entry.Timestamp; } catch (Exception e) { MigService.Log.Info(new MigEvent(this.GetName(), remoteAddress, "HTTP", request.HttpMethod.ToString(), String.Format("{0} {1} [ERROR: {2}]", response.StatusCode, request.RawUrl, e.Message))); connected = false; break; } } Thread.Sleep(100); } // there might be new data after sending } while (connected && bufferedData.Count > 0); // check if the remote end point is still alive every 15 seconds or so if (timeoutWatch.Elapsed.TotalSeconds > 15) { connected = connected && IsRemoteEndPointConnected(request.RemoteEndPoint); timeoutWatch.Stop(); timeoutWatch = Stopwatch.StartNew(); } } } else { try { MigClientRequest migRequest = null; if (url.StartsWith("api/")) { string message = url.Substring(url.IndexOf('/', 1) + 1); var migContext = new MigContext(ContextSource.WebServiceGateway, context); migRequest = new MigClientRequest(migContext, new MigInterfaceCommand(message)); // Disable HTTP caching response.Headers.Set(HttpResponseHeader.CacheControl, "no-cache, no-store, must-revalidate"); response.Headers.Set(HttpResponseHeader.Pragma, "no-cache"); response.Headers.Set(HttpResponseHeader.Expires, "0"); // Store POST data (if any) in the migRequest.RequestData field migRequest.RequestData = WebServiceUtility.ReadToEnd(request.InputStream); migRequest.RequestText = request.ContentEncoding.GetString(migRequest.RequestData); } OnPreProcessRequest(migRequest); bool requestHandled = (migRequest != null && migRequest.Handled); if (requestHandled) { SendResponseObject(context, migRequest.ResponseData); } else if (url.StartsWith(baseUrl) || baseUrl.Equals("/")) { // If request begins <base_url>, process as standard Web request string requestedFile = GetWebFilePath(url); if (!System.IO.File.Exists(requestedFile)) { response.StatusCode = (int)HttpStatusCode.NotFound; WebServiceUtility.WriteStringToContext(context, "<h1>404 - Not Found</h1>"); } else { bool isText = false; if (url.ToLower().EndsWith(".js")) // || requestedurl.EndsWith(".json")) { response.ContentType = "text/javascript"; isText = true; } else if (url.ToLower().EndsWith(".css")) { response.ContentType = "text/css"; isText = true; } else if (url.ToLower().EndsWith(".zip")) { response.ContentType = "application/zip"; } else if (url.ToLower().EndsWith(".png")) { response.ContentType = "image/png"; } else if (url.ToLower().EndsWith(".jpg")) { response.ContentType = "image/jpeg"; } else if (url.ToLower().EndsWith(".gif")) { response.ContentType = "image/gif"; } else if (url.ToLower().EndsWith(".svg")) { response.ContentType = "image/svg+xml"; } else if (url.ToLower().EndsWith(".mp3")) { response.ContentType = "audio/mp3"; } else if (url.ToLower().EndsWith(".wav")) { response.ContentType = "audio/x-wav"; } else if (url.ToLower().EndsWith(".appcache")) { response.ContentType = "text/cache-manifest"; } else if (url.ToLower().EndsWith(".otf") || url.ToLower().EndsWith(".ttf") || url.ToLower().EndsWith(".woff") || url.ToLower().EndsWith(".woff2")) { response.ContentType = "application/octet-stream"; } else if (url.ToLower().EndsWith(".xml")) { response.ContentType = "text/xml"; isText = true; } else { response.ContentType = "text/html"; isText = true; } var file = new System.IO.FileInfo(requestedFile); response.ContentLength64 = file.Length; bool modified = true; if (request.Headers.AllKeys.Contains("If-Modified-Since")) { var modifiedSince = DateTime.MinValue; DateTime.TryParse(request.Headers["If-Modified-Since"], out modifiedSince); if (file.LastWriteTime.ToUniversalTime().Equals(modifiedSince)) { modified = false; } } bool disableCacheControl = HttpCacheIgnoreCheck(url); if (!modified && !disableCacheControl) { // TODO: !IMPORTANT! exclude from caching files that contains SSI tags! response.StatusCode = (int)HttpStatusCode.NotModified; //!!DISABLED!! - The following line was preventing browser to load file from cache //response.Headers.Set(HttpResponseHeader.Date, file.LastWriteTimeUtc.ToString().Replace(",", ".")); } else { response.Headers.Set(HttpResponseHeader.LastModified, file.LastWriteTimeUtc.ToString().Replace(",", ".")); if (disableCacheControl) { response.Headers.Set(HttpResponseHeader.CacheControl, "no-cache, no-store, must-revalidate"); response.Headers.Set(HttpResponseHeader.Pragma, "no-cache"); response.Headers.Set(HttpResponseHeader.Expires, "0"); } else { response.Headers.Set(HttpResponseHeader.CacheControl, "max-age=86400"); } // PRE PROCESS text output if (isText) { try { WebFile webFile = GetWebFile(requestedFile); response.ContentEncoding = webFile.Encoding; response.ContentType += "; charset=" + webFile.Encoding.BodyName; // We don't need to parse the content again if it's coming from the cache if (!webFile.IsCached) { string body = webFile.Content; if (requestedFile.EndsWith(".md")) { // Built-in Markdown files support body = CommonMark.CommonMarkConverter.Convert(body); // TODO: add a way to include HTML header and footer template to be appended to the // TODO: translated markdown text } else { // HTML file // replace prepocessor directives with values bool tagFound; do { tagFound = false; int ts = body.IndexOf("{include "); if (ts >= 0) { int te = body.IndexOf("}", ts); if (te > ts) { string rs = body.Substring(ts + (te - ts) + 1); string cs = body.Substring(ts, te - ts + 1); string ls = body.Substring(0, ts); // try { if (cs.StartsWith("{include ")) { string fileName = cs.Substring(9).TrimEnd('}').Trim(); fileName = GetWebFilePath(fileName); // Encoding fileEncoding = DetectWebFileEncoding(fileName); if (fileEncoding == null) { fileEncoding = defaultWebFileEncoding; } var incFile = System.IO.File.ReadAllText(fileName, fileEncoding) + rs; body = ls + incFile; } } catch { body = ls + "<h5 style=\"color:red\">Error processing '" + cs.Replace("{", "[").Replace("}", "]") + "'</h5>" + rs; } tagFound = true; } } } while (tagFound); // continue if a pre processor tag was found // {hostos} body = body.Replace("{hostos}", Environment.OSVersion.Platform.ToString()); // {filebase} body = body.Replace("{filebase}", Path.GetFileNameWithoutExtension(requestedFile)); } // update the cache content with parsing results webFile.Content = body; } // Store the cache item if the file cache is enabled if (enableFileCache) { UpdateWebFileCache(requestedFile, webFile.Content, response.ContentEncoding); } // WebServiceUtility.WriteStringToContext(context, webFile.Content); } catch (Exception ex) { // TODO: report internal mig interface error response.StatusCode = (int)HttpStatusCode.InternalServerError; WebServiceUtility.WriteStringToContext(context, ex.Message + "\n" + ex.StackTrace); MigService.Log.Error(ex); } } else { WebServiceUtility.WriteBytesToContext(context, System.IO.File.ReadAllBytes(requestedFile)); } } } requestHandled = true; } OnPostProcessRequest(migRequest); if (!requestHandled && migRequest != null && migRequest.Handled) { SendResponseObject(context, migRequest.ResponseData); } else if (!requestHandled) { response.StatusCode = (int)HttpStatusCode.NotFound; WebServiceUtility.WriteStringToContext(context, "<h1>404 - Not Found</h1>"); } } catch (Exception eh) { // TODO: add error logging Console.Error.WriteLine(eh); } } connectionWatch.Stop(); logExtras = " [CLOSED AFTER " + Math.Round(connectionWatch.Elapsed.TotalSeconds, 3) + " seconds]"; } } else { response.StatusCode = (int)HttpStatusCode.Unauthorized; // this will only work in Linux (mono) //response.Headers.Set(HttpResponseHeader.WwwAuthenticate, "Basic"); // this works both on Linux and Windows //response.AddHeader("WWW-Authenticate", "Basic"); } } else { response.StatusCode = (int)HttpStatusCode.Unauthorized; // this will only work in Linux (mono) //response.Headers.Set(HttpResponseHeader.WwwAuthenticate, "Basic"); // this works both on Linux and Windows //response.AddHeader("WWW-Authenticate", "Basic"); } MigService.Log.Info(new MigEvent(this.GetName(), remoteAddress, "HTTP", request.HttpMethod.ToString(), String.Format("{0} {1}{2}", response.StatusCode, request.RawUrl, logExtras))); } catch (Exception ex) { MigService.Log.Error(ex); } finally { // // CleanUp/Dispose allocated resources // try { request.InputStream.Close(); } catch { // TODO: add logging } try { response.OutputStream.Close(); } catch { // TODO: add logging } try { response.Close(); } catch { // TODO: add logging } try { response.Abort(); } catch { // TODO: add logging } } }
public object InterfaceControl(MigInterfaceCommand request) { string response = ""; //default success value Commands command; Enum.TryParse <Commands>(request.Command.Replace(".", "_"), out command); switch (command) { case Commands.Remotes_Search: response = MigService.JsonSerialize(SearchRemotes(request.GetOption(0))); break; case Commands.Remotes_Add: { var remote = remotesData.Find(r => r.Manufacturer.ToLower() == request.GetOption(0).ToLower() && r.Model.ToLower() == request.GetOption(1).ToLower()); if (remote != null && remotesConfig.Find(r => r.Model.ToLower() == remote.Model.ToLower() && r.Manufacturer.ToLower() == remote.Manufacturer.ToLower()) == null) { var webClient = new WebClient(); string config = webClient.DownloadString("http://lirc.sourceforge.net/remotes/" + remote.Manufacturer + "/" + remote.Model); remote.Configuration = GetBytes(config); remotesConfig.Add(remote); SaveConfig(); } } break; case Commands.Remotes_Remove: { var remote = remotesConfig.Find(r => r.Manufacturer.ToLower() == request.GetOption(0).ToLower() && r.Model.ToLower() == request.GetOption(1).ToLower()); if (remote != null) { remotesConfig.Remove(remote); SaveConfig(); } } break; case Commands.Remotes_List: response = MigService.JsonSerialize(remotesConfig); break; case Commands.Control_IrSend: string commands = ""; int c = 0; while (request.GetOption(c) != "") { var options = request.GetOption(c).Split('/'); foreach (string o in options) { commands += "\"" + o + "\" "; } c++; } MigService.ShellCommand("irsend", "SEND_ONCE " + commands); break; } return(response); }
public static void Main(string[] args) { string webPort = "8088"; Console.WriteLine("MigService test APP"); Console.WriteLine("URL: http://localhost:{0}", webPort); var migService = new MigService(); // Add and configure the Web gateway var web = migService.AddGateway("WebServiceGateway"); web.SetOption("HomePath", "html"); web.SetOption("BaseUrl", "/pages/"); web.SetOption("Host", "*"); web.SetOption("Port", webPort); web.SetOption("Password", ""); web.SetOption("EnableFileCaching", "False"); // Add and configure the Web Socket gateway var ws = migService.AddGateway("WebSocketGateway"); ws.SetOption("Port", "8181"); migService.StartService(); migService.RegisterApi("myapp/demo", (request) => { Console.WriteLine("Received API call from source {0}\n", request.Context.Source); Console.WriteLine("[Context data]\n{0}\n", MigService.JsonSerialize(request.Context.Data, true)); Console.WriteLine("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true)); var cmd = request.Command; // cmd.Domain is the first element in the API URL (myapp) // cmd.Address is the second element in the API URL (demo) // cmd.Command is the third element in the API URL (greet | echo | ping) // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n) switch (cmd.Command) { case "greet": var name = cmd.GetOption(0); migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Greet", "Greet.User", name); break; case "echo": string fullRequestPath = cmd.OriginalRequest; migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Echo", "Echo.Data", fullRequestPath); break; case "ping": migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Ping", "Ping.Reply", "PONG"); break; } return(new ResponseStatus(Status.Ok)); }); while (true) { Thread.Sleep(10000); } }
public static void Main(string[] args) { var Log = MigService.Log; string webServicePort = "8088"; string webSocketPort = "8181"; string authUser = "******"; string authPass = "******"; string authRealm = "MIG Secure Zone"; Log.Info("MigService test APP"); Log.Info("URL: http://localhost:{0}", webServicePort); var migService = new MigService(); // Add and configure the WebService gateway var web = (WebServiceGateway)migService.AddGateway(Gateways.WebServiceGateway); web.SetOption(WebServiceGatewayOptions.HomePath, "html"); web.SetOption(WebServiceGatewayOptions.BaseUrl, "/pages/"); // for deploying modern web app (eg. Angular 2 apps) web.SetOption(WebServiceGatewayOptions.UrlAliasPrefix, "app/*:app/index.html"); web.SetOption(WebServiceGatewayOptions.Host, "*"); web.SetOption(WebServiceGatewayOptions.Port, webServicePort); if (!String.IsNullOrEmpty(authUser) && !String.IsNullOrEmpty(authPass)) { //web.SetOption(WebServiceGatewayOptions.Authentication, WebAuthenticationSchema.Basic); web.SetOption(WebServiceGatewayOptions.Authentication, WebAuthenticationSchema.Digest); web.SetOption(WebServiceGatewayOptions.AuthenticationRealm, authRealm); web.UserAuthenticationHandler += (sender, eventArgs) => { if (eventArgs.Username == authUser) { // WebServiceGateway requires password to be encrypted using the `Digest.CreatePassword(..)` method. // This applies both to 'Digest' and 'Basic' authentication methods. string password = Digest.CreatePassword(authUser, authRealm, authPass); return(new User(authUser, authRealm, password)); } return(null); }; } web.SetOption(WebServiceGatewayOptions.EnableFileCaching, "False"); // Add and configure the WebSocket gateway var ws = (WebSocketGateway)migService.AddGateway(Gateways.WebSocketGateway); ws.SetOption(WebSocketGatewayOptions.Port, webSocketPort); // WebSocketGateway access via authorization token ws.SetOption(WebSocketGatewayOptions.Authentication, WebAuthenticationSchema.Token); /* * if (!String.IsNullOrEmpty(authUser) && !String.IsNullOrEmpty(authPass)) * { * //ws.SetOption(WebSocketGatewayOptions.Authentication, WebAuthenticationSchema.Basic); * ws.SetOption(WebSocketGatewayOptions.Authentication, WebAuthenticationSchema.Digest); * ws.SetOption(WebSocketGatewayOptions.AuthenticationRealm, authRealm); * ((WebSocketGateway) ws).UserAuthenticationHandler += (sender, eventArgs) => * { * if (eventArgs.Username == authUser) * { * return new User(authUser, authRealm, authPass); * } * return null; * }; * } */ migService.StartService(); // API commands and events are exposed to all active gateways (WebService and WebSocket in this example) migService.RegisterApi("myapp/demo", (request) => { Log.Debug("Received API call over {0}\n", request.Context.Source); Log.Debug("[Context data]\n{0}\n", request.Context.Data); Log.Debug("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true)); var cmd = request.Command; // cmd.Domain is the first element in the API URL (myapp) // cmd.Address is the second element in the API URL (demo) // cmd.Command is the third element in the API URL (greet | echo | ping) // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n) switch (cmd.Command) { case ApiCommands.Token: // authorization token will expire in 5 seconds var token = ws.GetAuthorizationToken(5); return(new ResponseText(token.Value)); case ApiCommands.Greet: var name = cmd.GetOption(0); migService.RaiseEvent( typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Greet", "Greet.User", name ); break; case ApiCommands.Echo: string fullRequestPath = cmd.OriginalRequest; migService.RaiseEvent( typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Echo", "Echo.Data", fullRequestPath ); break; case ApiCommands.Ping: migService.RaiseEvent( typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Ping", "Ping.Reply", "PONG" ); break; } return(new ResponseStatus(Status.Ok)); }); while (true) { Thread.Sleep(5000); } }