-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.ashx
72 lines (64 loc) · 1.59 KB
/
data.ashx
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
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Threading;
using System.Collections.Generic;
public class Handler : IHttpHandler
{
public class data
{
public double x;
public double y1;
public double y2;
public double y3;
public double y4;
public double y5;
public double y6;
public double y7;
public double y8;
public double y9;
}
public void ProcessRequest(HttpContext context)
{
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
context.Response.ContentType = "text/event-stream";
context.Response.CacheControl = "no-cache";
context.Response.Flush();
var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
List<data> _data = new List<data>();
Random r = new Random();
while (context.Response.IsClientConnected)
{
_data.Clear();
for (int i = 0; i < 100; i++)
{
data d = new data();
d.x = r.NextDouble();
d.y1 = r.NextDouble();
d.y2 = r.NextDouble();
d.y3 = r.NextDouble();
d.y4 = r.NextDouble();
d.y5 = r.NextDouble();
d.y6 = r.NextDouble();
d.y7 = r.NextDouble();
d.y8 = r.NextDouble();
d.y9 = r.NextDouble();
_data.Add(d);
}
string result = ser.Serialize(_data);
string s1 = string.Format("event: {0}\n", "message");
string s2 = string.Format("data: {0}\n\n", result);
context.Response.Write(s1);
context.Response.Write(s2);
context.Response.Flush();
Thread.Sleep(1000);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}