source: uKadecot/trunk/tools/EcnlControllerUI/NiseWebSocket/NiseWebSocket.cs@ 101

Last change on this file since 101 was 101, checked in by coas-nagasima, 9 years ago

TOPPERS/uKadecotのソースコードを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/plain
File size: 4.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Html;
4using System.Net.WebSockets;
5using System.Runtime.CompilerServices;
6using System.Web;
7using jQueryApi;
8
9public class NiseEvent
10{
11 public string type;
12
13 public NiseEvent(string type)
14 {
15 this.type = type;
16 }
17}
18
19public class MessageEvent : NiseEvent
20{
21 public string data;
22
23 public MessageEvent(string data)
24 : base("message")
25 {
26 this.data = data;
27 }
28}
29
30public class CloseEvent : NiseEvent
31{
32 public const ushort CLOSE_NORMAL = 1000;
33 public const ushort CLOSE_GOING_AWAY = 1001;
34 public const ushort CLOSE_PROTOCOL_ERROR = 1002;
35 public const ushort CLOSE_UNSUPPORTED = 1003;
36 public const ushort CLOSE_TOO_LARGE = 1004;
37 public const ushort CLOSE_NO_STATUS = 1005;
38 public const ushort CLOSE_ABNORMAL = 1006;
39
40 public ushort code;
41 public string reason;
42
43 public CloseEvent(ushort code, string reason)
44 : base("close")
45 {
46 this.code = code;
47 this.reason = reason;
48 }
49}
50
51public delegate void NiseHtmlEventHandler(NiseEvent @event);
52
53public class NiseWebSocket
54{
55 public const ushort CLOSED = (ushort)ReadyState.Closed;
56 public const ushort CLOSING = (ushort)ReadyState.Closing;
57 public const ushort CONNECTING = (ushort)ReadyState.Connecting;
58 public const ushort OPEN = (ushort)ReadyState.Open;
59
60 string _Url;
61 string[] _Protocols;
62 ReadyState _ReadyState = ReadyState.Open;
63 int _PollingTimer;
64
65 public NiseWebSocket(string url)
66 {
67 int opentimer = 0;
68 _ReadyState = ReadyState.Connecting;
69 _Url = url.ReplaceFirst("ws://", "http://");
70 opentimer = Window.SetTimeout(() =>
71 {
72 Window.ClearTimeout(opentimer);
73 Opend();
74 }, 1000);
75 }
76
77 private void Opend()
78 {
79 _ReadyState = ReadyState.Open;
80 NiseEvent oe = new NiseEvent("open");
81 if (OnOpen != null)
82 OnOpen(oe);
83
84 _PollingTimer = Window.SetInterval(() =>
85 {
86 jQuery.Ajax(new jQueryAjaxOptions()
87 {
88 Url = _Url,
89 Type = "POST",
90 DataType = "json",
91 Data = "{\"brk_wai\":\"Polling\"}",
92 Success = (respo, textStatus, request) =>
93 {
94 if (respo != null) {
95 MessageEvent e = new MessageEvent(request.ResponseText);
96 if (OnMessage != null)
97 OnMessage(e);
98 }
99 },
100 Error = (request, textStatus, error) =>
101 {
102 }
103 });
104 }, 1000);
105 }
106
107 public NiseWebSocket(string url, string protocols)
108 : this(url)
109 {
110 _Protocols = protocols.Split(',');
111 }
112
113 public NiseWebSocket(string url, string[] protocols)
114 : this(url)
115 {
116 _Protocols = protocols;
117 }
118
119 [IntrinsicProperty]
120 public BinaryType BinaryType { get; set; }
121 [IntrinsicProperty]
122 public int BufferedAmount { get; private set; }
123 [IntrinsicProperty]
124 public string Extensions { get; private set; }
125 [IntrinsicProperty]
126 [ScriptName("onclose")]
127 public NiseHtmlEventHandler OnClose { get; set; }
128 [IntrinsicProperty]
129 [ScriptName("onerror")]
130 public NiseHtmlEventHandler OnError { get; set; }
131 [IntrinsicProperty]
132 [ScriptName("onmessage")]
133 public NiseHtmlEventHandler OnMessage { get; set; }
134 [IntrinsicProperty]
135 [ScriptName("onopen")]
136 public NiseHtmlEventHandler OnOpen { get; set; }
137 [IntrinsicProperty]
138 public string Protocol { get { return String.Join(",", _Protocols); } }
139 [IntrinsicProperty]
140 public ReadyState ReadyState { get { return _ReadyState; } }
141 [IntrinsicProperty]
142 public string Url { get { return _Url; } }
143
144 public void Close()
145 {
146 if (_PollingTimer != 0) {
147 Window.ClearInterval(_PollingTimer);
148 _PollingTimer = 0;
149 }
150
151 int closetimer = 0;
152 _ReadyState = ReadyState.Closing;
153
154 closetimer = Window.SetTimeout(() =>
155 {
156 Window.ClearTimeout(closetimer);
157 Closed(CloseEvent.CLOSE_NORMAL, "");
158 }, 1000);
159 }
160
161 private void Closed(ushort code, string reason)
162 {
163 _ReadyState = ReadyState.Closed;
164 CloseEvent oe = new CloseEvent(code, reason);
165 if (OnClose != null)
166 OnClose(oe);
167 }
168
169 public void Send(string data)
170 {
171 jQuery.Ajax(new jQueryAjaxOptions()
172 {
173 Url = _Url,
174 Type = "POST",
175 Data = data,
176 DataType = "json",
177 Success = (respo, textStatus, request) =>
178 {
179 if (respo != null) {
180 MessageEvent e = new MessageEvent(request.ResponseText);
181 if (OnMessage != null)
182 OnMessage(e);
183 }
184 },
185 Error = (request, textStatus, error) =>
186 {
187 NiseEvent e = new NiseEvent("error");
188 OnError(e);
189 }
190 });
191 }
192}
Note: See TracBrowser for help on using the repository browser.