WebSocket周り修正
@@ -0,0 +1,68 @@ | ||
1 | +package socket_action; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.io.InputStream; | |
5 | +import java.io.OutputStream; | |
6 | + | |
7 | +import servercore.http.HttpRequest; | |
8 | + | |
9 | +public class WebTest2 { | |
10 | + private OutputStream ops = null; | |
11 | + private InputStream ips = null; | |
12 | + public boolean cont = true; | |
13 | + public static String A = ""; | |
14 | + private String B = null; | |
15 | + public void execute(HttpRequest req, OutputStream ops, InputStream ips) { | |
16 | + this.ops = ops; | |
17 | + this.ips = ips; | |
18 | + System.out.println("\r\nWebTest start"); | |
19 | + B = WebTest2.A; | |
20 | + SyncsThread th = new SyncsThread(); | |
21 | + th.start(); | |
22 | + try { | |
23 | + while(cont) { | |
24 | + WebTest2.A = read(); | |
25 | + //send(str); | |
26 | + } | |
27 | + } catch(IOException e) { | |
28 | + e.printStackTrace(); | |
29 | + } | |
30 | + System.out.println("\r\nWebTest end"); | |
31 | + } | |
32 | + class SyncsThread extends Thread { | |
33 | + public void run() { | |
34 | + try { | |
35 | + while(true){ | |
36 | + if(WebTest2.A != B) { | |
37 | + send(WebTest2.A); | |
38 | + B = WebTest2.A; | |
39 | + } | |
40 | + Thread.sleep(10); | |
41 | + } | |
42 | + }catch(Exception e) { | |
43 | + e.fillInStackTrace(); | |
44 | + } | |
45 | + | |
46 | + } | |
47 | + } | |
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 | +} |
@@ -12,29 +12,29 @@ | ||
12 | 12 | public void execute(HttpRequest req, OutputStream ops, InputStream ips) { |
13 | 13 | this.ops = ops; |
14 | 14 | System.out.println("\r\nWebTest start"); |
15 | - while(cont) { | |
16 | - try { | |
17 | - Thread.sleep(2000); | |
18 | - send(String.valueOf(System.currentTimeMillis())); | |
19 | - } catch (InterruptedException e) { | |
20 | - // TODO 自動生成された catch ブロック | |
21 | - e.printStackTrace(); | |
15 | + try { | |
16 | + while(cont) { | |
17 | + try { | |
18 | + Thread.sleep(2000); | |
19 | + send(String.valueOf(System.currentTimeMillis())); | |
20 | + } catch (InterruptedException e) { | |
21 | + e.printStackTrace(); | |
22 | + } | |
22 | 23 | } |
24 | + } catch(IOException e) { | |
25 | + e.printStackTrace(); | |
23 | 26 | } |
24 | 27 | System.out.println("\r\nWebTest end"); |
25 | 28 | } |
26 | 29 | |
27 | - public static void send(String str) { | |
30 | + public static void send(String str) throws IOException { | |
28 | 31 | if(ops == null) { |
29 | 32 | return; |
30 | 33 | } |
31 | - try { | |
32 | - ops.write(0x00); | |
33 | - ops.write(str.getBytes()); | |
34 | - ops.write(0xff); | |
35 | - ops.flush(); | |
36 | - } catch (IOException e) { | |
37 | - e.printStackTrace(); | |
38 | - } | |
34 | + ops.write(0x00); | |
35 | + ops.write(str.getBytes()); | |
36 | + ops.write(0xff); | |
37 | + ops.flush(); | |
38 | + | |
39 | 39 | } |
40 | 40 | } |
@@ -28,6 +28,8 @@ | ||
28 | 28 | HttpRequest req = analyzeReq(ips); |
29 | 29 | req.normarizePath(HttpServer.getRootDirPath()); |
30 | 30 | String ext = req.getExtesion(); |
31 | + | |
32 | + // レスポンスを圧縮(gzip)して送信するか | |
31 | 33 | boolean useGzip = useEncodingOK(req.getHeader().get("Accept-Encoding"), |
32 | 34 | HttpServer.getUseCompress()); |
33 | 35 |
@@ -113,11 +113,11 @@ | ||
113 | 113 | |
114 | 114 | Class<Action> cls = null; |
115 | 115 | |
116 | - cls = (Class<Action>)Class.forName(PackageName); | |
117 | - Method method = cls.getMethod("execute", HttpRequest.class, OutputStream.class, InputStream.class); | |
118 | - // 実行する | |
119 | - method.invoke(cls.newInstance(), req, ops, ips); | |
120 | - } | |
116 | + cls = (Class<Action>)Class.forName(PackageName); | |
117 | + Method method = cls.getMethod("execute", HttpRequest.class, OutputStream.class, InputStream.class); | |
118 | + // 実行する | |
119 | + method.invoke(cls.newInstance(), req, ops, ips); | |
120 | + } | |
121 | 121 | public static byte[] getChalengeKey(byte[] header) { |
122 | 122 | byte[] ret = null; |
123 | 123 | try { |
@@ -0,0 +1,38 @@ | ||
1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" | |
2 | + "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
3 | + | |
4 | +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> | |
5 | +<head> | |
6 | +<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"/> | |
7 | +<meta http-equiv="Content-Style-Type" content="text/css" /> | |
8 | +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
9 | +<meta http-equiv="Content-Script-Type" content="text/javascript" /> | |
10 | + | |
11 | +<script type="text/javascript" src="js/jquery.js"></script> | |
12 | +<script> | |
13 | +<!-- | |
14 | +var host = location.host; | |
15 | +var ws = new WebSocket("ws://" + host + "/WebTest2.ws"); | |
16 | +ws.onmessage = function(msg) { | |
17 | + document.getElementById('test').innerHTML = "<p>" + msg.data + "</p>"; | |
18 | +} | |
19 | +ws.onopen = function(){ | |
20 | + ws.send('message'); | |
21 | + document.getElementById('massage').innerHTML = "通信開始"; | |
22 | +} | |
23 | +function ch(){ | |
24 | + ws.send(document.getElementById('txt').value); | |
25 | +} | |
26 | + --> | |
27 | +</script> | |
28 | +<title>テキストの共同編集</title> | |
29 | + | |
30 | +</head> | |
31 | + | |
32 | +<body> | |
33 | +<p>これはメイン画面です。</p> | |
34 | +<p id="massage"></p> | |
35 | +<div id="test"></div> | |
36 | +<textarea id="txt" onchange="ch()"></textarea> | |
37 | +</body> | |
38 | +</html> |
@@ -11,7 +11,8 @@ | ||
11 | 11 | <script type="text/javascript" src="js/jquery.js"></script> |
12 | 12 | <script> |
13 | 13 | <!-- |
14 | -var ws = new WebSocket("ws://localhost/WebTest.ws"); | |
14 | +var host = location.host; | |
15 | +var ws = new WebSocket("ws://" + host + "/WebTest.ws"); | |
15 | 16 | ws.onmessage = function(msg) { |
16 | 17 | document.getElementById('test').innerHTML += "<p>" + msg.data + "</p>"; |
17 | 18 | } |