public static MseService Instance(int port) { if (instance == null) { instance = new MseService(port); } return(instance); }
/// <summary> /// 工作线程,启动 httpListener .... /// </summary> void proc_run() { evt_.Set(); int port = 3300; if (config_.ContainsKey("server_port")) { BindingUrl = "http://*:" + config_["server_port"] + "/"; port = int.Parse(config_["server_port"]); } Log.log("HttpServer starting at port=" + port); MseService.Instance(port); HttpListener listener = new HttpListener(); listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; listener.Prefixes.Add(BindingUrl); try { listener.Start(); Console.WriteLine("启动服务,使用 " + BindingUrl); Log.log(" ok"); } catch (Exception e) { Console.WriteLine("[MyHttpServer] to start " + BindingUrl + " exception: " + e.Message); Log.log("HttpServer start ERR: msg=" + e.Message); throw new Exception("启动失败,请检查日志"); } Console.WriteLine("[MyHttpServer] starting " + BindingUrl + " OK!"); bool quit = false; do { HttpListenerContext ctx = listener.GetContext(); HttpListenerRequest req = ctx.Request; HttpListenerResponse res = ctx.Response; if (req.Url.AbsolutePath == "/cmd/killme") { quit = true; try { res.StatusCode = 200; StreamWriter sw = new StreamWriter(res.OutputStream); sw.Write("OK: server will be killed."); sw.Close(); //res.Close(); } catch { } } else { Handler(req, res); res.Close(); } } while (!quit); }
public static MseService Instance(int port) { if (instance == null) instance = new MseService(port); return instance; }