-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWebServer.cs
117 lines (102 loc) · 4.24 KB
/
WebServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using pringApp;
namespace PrintApp
{
class WebServer : HttpServer
{
public WebServer(int port) : base(port)
{
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override void handleGETRequest(HttpProcessor p)
{
p.writeSuccess();
Response res = new Response();
if (p.http_url.Equals("/print.html"))
{
p.outputStream.WriteLine("<!DOCTYPE html>");
p.outputStream.WriteLine("<html lang='zh-CN'><head>");
p.outputStream.WriteLine("<meta charset='UTF-8'>");
p.outputStream.WriteLine("<script src='http://ad.ddyunf.com/resources/printer/printer.js'></script> ");
p.outputStream.WriteLine("</head>");
p.outputStream.WriteLine("<body>");
p.outputStream.WriteLine("<script type=text/javascript>");
p.outputStream.WriteLine("window.addEventListener('message', function(e){");
p.outputStream.WriteLine("postData(e);");
p.outputStream.WriteLine("}, false)");
p.outputStream.WriteLine("</script>");
p.outputStream.WriteLine("</body></html>");
}else {
res.stauts = ResStatusCode.NoGet.ToString();
res.desc = "不支持get方式";
string json = JsonConvert.SerializeObject(res);
p.outputStream.WriteLine(json);
}
}
public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData)
{
Config config = new Config();
string priterNameConfig = config.ReadString("printer", "print", null);
if (priterNameConfig == null || priterNameConfig.Length == 0)
{
p.writeSuccess("application/json");
Response res = new Response();
res.stauts = ResStatusCode.NO_Printer.ToString();
res.desc = "请先设置打印面单机器";
string json = JsonConvert.SerializeObject(res);
p.outputStream.WriteLine(json);
}
else {
HandleDelegate handleDelegate = asynHandle;
string data = inputData.ReadToEnd();
try
{
Console.WriteLine("======data===========================" + data);
JArray item = (JArray)JsonConvert.DeserializeObject(data);
IAsyncResult result = handleDelegate.BeginInvoke(item, null, null);
p.writeSuccess("application/json");
Response res = new Response();
res.stauts = ResStatusCode.Success.ToString();
string json = JsonConvert.SerializeObject(res);
p.outputStream.WriteLine(json);
}
catch (Newtonsoft.Json.JsonReaderException e)
{
p.writeSuccess("application/json");
Response res = new Response();
res.stauts = ResStatusCode.Forma_Error.ToString();
res.desc = "数据格式错误";
string json = JsonConvert.SerializeObject(res);
p.outputStream.WriteLine(json);
}
}
}
public delegate void HandleDelegate(JArray item );
public static void asynHandle(JArray item )
{
Config config = new Config();
PrintSetting printSetting = new PrintSetting();
string priterNameConfig = config.ReadString("printer", "print", null);
if (priterNameConfig != null && priterNameConfig.Length > 0)
{
printSetting.priterName = priterNameConfig;
}
PrintService printService = new PrintService(printSetting, item);
printService.Print();
}
public override string ToString()
{
return base.ToString();
}
}
}