WebSocket周りのソース整理
@@ -4,17 +4,16 @@ | ||
4 | 4 | import java.io.InputStream; |
5 | 5 | import java.io.OutputStream; |
6 | 6 | |
7 | +import servercore.html5.WebSocketFrame; | |
7 | 8 | import servercore.http.HttpRequest; |
8 | 9 | |
9 | -public class WebTest2 { | |
10 | - private OutputStream ops = null; | |
11 | - private InputStream ips = null; | |
10 | +public class WebTest2 extends WebSocketFrame{ | |
12 | 11 | public boolean cont = true; |
13 | 12 | public static String A = ""; |
14 | 13 | private String B = null; |
15 | 14 | public void execute(HttpRequest req, OutputStream ops, InputStream ips) { |
16 | - this.ops = ops; | |
17 | - this.ips = ips; | |
15 | + super.ops = ops; | |
16 | + super.ips = ips; | |
18 | 17 | System.out.println("\r\nWebTest start"); |
19 | 18 | B = WebTest2.A; |
20 | 19 | SyncsThread th = new SyncsThread(); |
@@ -42,27 +41,6 @@ | ||
42 | 41 | }catch(Exception e) { |
43 | 42 | e.fillInStackTrace(); |
44 | 43 | } |
45 | - | |
46 | 44 | } |
47 | 45 | } |
48 | - public String read() throws IOException{ | |
49 | - byte[] data = new byte[8098]; | |
50 | - int len = ips.read(data); | |
51 | - String str = ""; | |
52 | - if(data[0] == 0 && data[len - 1] == -1) { | |
53 | - str = new String(data, 1, len - 2); | |
54 | - } | |
55 | - return str; | |
56 | - } | |
57 | - | |
58 | - public void send(String str) throws IOException { | |
59 | - if(ops == null) { | |
60 | - return; | |
61 | - } | |
62 | - ops.write(0x00); | |
63 | - ops.write(str.getBytes()); | |
64 | - ops.write(0xff); | |
65 | - ops.flush(); | |
66 | - | |
67 | - } | |
68 | 46 | } |
@@ -5,20 +5,19 @@ | ||
5 | 5 | import java.io.OutputStream; |
6 | 6 | |
7 | 7 | import servercore.http.HttpRequest; |
8 | +import servercore.html5.WebSocketFrame; | |
8 | 9 | |
9 | -public class WebTest { | |
10 | - private static OutputStream ops = null; | |
11 | - private static InputStream ips = null; | |
10 | +public class WebTest extends WebSocketFrame{ | |
12 | 11 | public boolean cont = true; |
13 | 12 | public void execute(HttpRequest req, OutputStream ops, InputStream ips) { |
14 | - this.ops = ops; | |
15 | - this.ips = ips; | |
13 | + super.ops = ops; | |
14 | + super.ips = ips; | |
16 | 15 | System.out.println("\r\nWebTest start"); |
17 | 16 | try { |
18 | 17 | System.out.println("READ:" + read()); |
19 | 18 | while(cont) { |
20 | 19 | try { |
21 | - Thread.sleep(500); | |
20 | + Thread.sleep(1000); | |
22 | 21 | send(String.valueOf(System.currentTimeMillis())); |
23 | 22 | } catch (InterruptedException e) { |
24 | 23 | e.printStackTrace(); |
@@ -30,55 +29,4 @@ | ||
30 | 29 | System.out.println("\r\nWebTest end"); |
31 | 30 | } |
32 | 31 | |
33 | - public String read() throws IOException{ | |
34 | - byte[] data = new byte[8098]; | |
35 | - int len = ips.read(data); | |
36 | -/* | |
37 | - for(int i = 0;i < len;i++) { | |
38 | - System.out.printf("%2d:%#x\n", i, data[i]); | |
39 | - } | |
40 | -*/ | |
41 | - String str = new String(data, 0, len); | |
42 | - if(len < 8) { | |
43 | - return ""; | |
44 | - } | |
45 | - boolean isFinalPacket = (data[0] & 0x80) == 0x80; | |
46 | - int opCode = data[0] & 0x15; | |
47 | - String opCodeType = ""; | |
48 | - switch(opCode) { | |
49 | - case 0x01: | |
50 | - opCodeType = "text"; | |
51 | - break; | |
52 | - default: | |
53 | - break; | |
54 | - } | |
55 | - boolean masked = (data[1] & 0x80) == 0x80; | |
56 | - byte payloadLength = (byte)(data[1] & 0x7f); | |
57 | - if(payloadLength == 0x7e) { | |
58 | - // next Uint16 | |
59 | - } else if(payloadLength == 0x7f) { | |
60 | - // nexy Uint64 | |
61 | - } | |
62 | - byte[] maskingKey = new byte[4]; | |
63 | - maskingKey[0] = data[2]; | |
64 | - maskingKey[1] = data[3]; | |
65 | - maskingKey[2] = data[4]; | |
66 | - maskingKey[3] = data[5]; | |
67 | - byte[] appData = new byte[payloadLength]; | |
68 | - for(int i = 0;i < payloadLength;i++) { | |
69 | - appData[i] = (byte)(data[6 + i] ^ maskingKey[i % 4]); | |
70 | - | |
71 | - } | |
72 | - return new String(appData); | |
73 | - } | |
74 | - public static void send(String str) throws IOException { | |
75 | - if(ops == null) { | |
76 | - return; | |
77 | - } | |
78 | - ops.write(0x81); | |
79 | - ops.write((byte)str.length()); | |
80 | - ops.write(str.getBytes()); | |
81 | - ops.flush(); | |
82 | - | |
83 | - } | |
84 | 32 | } |
@@ -0,0 +1,62 @@ | ||
1 | +package servercore.html5; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.io.InputStream; | |
5 | +import java.io.OutputStream; | |
6 | + | |
7 | +public class WebSocketFrame { | |
8 | + protected static OutputStream ops = null; | |
9 | + protected static InputStream ips = null; | |
10 | + | |
11 | + public String read() throws IOException{ | |
12 | + byte[] data = new byte[8098]; | |
13 | + int len = ips.read(data); | |
14 | +/* | |
15 | + for(int i = 0;i < len;i++) { | |
16 | + System.out.printf("%2d:%#x\n", i, data[i]); | |
17 | + } | |
18 | +*/ | |
19 | + String str = new String(data, 0, len); | |
20 | + if(len < 8) { | |
21 | + return ""; | |
22 | + } | |
23 | + boolean isFinalPacket = (data[0] & 0x80) == 0x80; | |
24 | + int opCode = data[0] & 0x15; | |
25 | + String opCodeType = ""; | |
26 | + switch(opCode) { | |
27 | + case 0x01: | |
28 | + opCodeType = "text"; | |
29 | + break; | |
30 | + default: | |
31 | + break; | |
32 | + } | |
33 | + boolean masked = (data[1] & 0x80) == 0x80; | |
34 | + byte payloadLength = (byte)(data[1] & 0x7f); | |
35 | + if(payloadLength == 0x7e) { | |
36 | + // next Uint16 | |
37 | + } else if(payloadLength == 0x7f) { | |
38 | + // nexy Uint64 | |
39 | + } | |
40 | + byte[] maskingKey = new byte[4]; | |
41 | + maskingKey[0] = data[2]; | |
42 | + maskingKey[1] = data[3]; | |
43 | + maskingKey[2] = data[4]; | |
44 | + maskingKey[3] = data[5]; | |
45 | + byte[] appData = new byte[payloadLength]; | |
46 | + for(int i = 0;i < payloadLength;i++) { | |
47 | + appData[i] = (byte)(data[6 + i] ^ maskingKey[i % 4]); | |
48 | + | |
49 | + } | |
50 | + return new String(appData); | |
51 | + } | |
52 | + public static void send(String str) throws IOException { | |
53 | + if(ops == null) { | |
54 | + return; | |
55 | + } | |
56 | + ops.write(0x81); | |
57 | + ops.write((byte)str.length()); | |
58 | + ops.write(str.getBytes()); | |
59 | + ops.flush(); | |
60 | + | |
61 | + } | |
62 | +} |
@@ -78,7 +78,6 @@ | ||
78 | 78 | private static void executeWebSokcet(HttpRequest req, String PackageName, OutputStream ops, InputStream ips) throws Exception { |
79 | 79 | |
80 | 80 | StringBuffer sb = new StringBuffer(); |
81 | - System.out.println(req.getHeader()); | |
82 | 81 | String webSocketVersion = req.getHeader().get("Sec-WebSocket-Version"); |
83 | 82 | String webSocketProtocol = req.getHeader().get("Sec-websocket-protocol"); |
84 | 83 | String[] protocols = webSocketProtocol != null ? webSocketProtocol.split(",") : new String[0]; |
@@ -89,8 +88,6 @@ | ||
89 | 88 | |
90 | 89 | String path = req.getRowPath(); |
91 | 90 | String key1 = req.getHeader().get("Sec-WebSocket-Key").trim(); |
92 | - //byte[] key2 = new BASE64Decoder().decodeBuffer(key1); | |
93 | - //key1 = new String(key2, 0, key2.length); | |
94 | 91 | |
95 | 92 | MessageDigest md = MessageDigest.getInstance("SHA1"); |
96 | 93 | md.update((key1 + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes()); |
@@ -114,71 +111,4 @@ | ||
114 | 111 | // 実行する |
115 | 112 | method.invoke(cls.newInstance(), req, ops, ips); |
116 | 113 | } |
117 | - public static byte[] getChalengeKey(byte[] header) { | |
118 | - byte[] ret = null; | |
119 | - try { | |
120 | - for(int i = 0;i < header.length;i++) { | |
121 | - if(header[i] == '\r' && header[i + 1] == '\n') { | |
122 | - if(header[i + 2] == '\r' && header[i + 3] == '\n') { | |
123 | - int lastAt = header.length - (i + 4); | |
124 | - ret = new byte[lastAt]; | |
125 | - System.arraycopy(header, i + 4, ret, 0, lastAt); | |
126 | - break; | |
127 | - } | |
128 | - } | |
129 | - } | |
130 | - } catch(Exception e) { | |
131 | - e.printStackTrace(); | |
132 | - } | |
133 | - return ret; | |
134 | - } | |
135 | - public static long chooseNum(String str) { | |
136 | - String code = ""; | |
137 | - for(int i = 0;i < str.length();i++) { | |
138 | - try { | |
139 | - int num = Integer.parseInt("" + str.charAt(i)); | |
140 | - code += num; | |
141 | - } catch(Exception e) {} | |
142 | - } | |
143 | - long ret = 0; | |
144 | - try { | |
145 | - ret = Long.parseLong(code); | |
146 | - } catch(Exception e) {} | |
147 | - return ret; | |
148 | - } | |
149 | - public static int chooseSpace(String str) { | |
150 | - int ret = 0; | |
151 | - for(int i =0;i < str.length();i++) { | |
152 | - if(str.charAt(i) == ' ') { | |
153 | - ret++; | |
154 | - } | |
155 | - } | |
156 | - return ret; | |
157 | - } | |
158 | - public static byte[] bigendiean(long key1_w, long key2_w) { | |
159 | - long lon1 = key1_w; | |
160 | - long lon2 = key2_w; | |
161 | - byte[] bytes = new byte[8]; | |
162 | - int[] ints = new int[8]; | |
163 | - ints[3] = (int)(lon1 % 256); | |
164 | - lon1 /= 256; | |
165 | - ints[2] = (int)(lon1 % 256); | |
166 | - lon1 /= 256; | |
167 | - ints[1] = (int)(lon1 % 256); | |
168 | - lon1 /= 256; | |
169 | - ints[0] = (int)lon1; | |
170 | - | |
171 | - ints[7] = (int)(lon2 % 256); | |
172 | - lon2 /= 256; | |
173 | - ints[6] = (int)(lon2 % 256); | |
174 | - lon2 /= 256; | |
175 | - ints[5] = (int)(lon2 % 256); | |
176 | - lon2 /= 256; | |
177 | - ints[4] = (int)lon2; | |
178 | - | |
179 | - for(int i = 0;i < 8;i++) { | |
180 | - bytes[i] = (byte)ints[i]; | |
181 | - } | |
182 | - return bytes; | |
183 | - } | |
184 | 114 | } |