• R/O
  • SSH
  • HTTPS

nicolib: Commit


Commit MetaInfo

Revision43 (tree)
Time2010-03-08 02:15:46
Authormomentspace

Log Message

(empty log message)

Change Summary

Incremental Difference

--- Java/Nico-Lib-Java-Talisker@ZQN/src/namaComment/NamaComment.java (revision 42)
+++ Java/Nico-Lib-Java-Talisker@ZQN/src/namaComment/NamaComment.java (revision 43)
@@ -1,11 +1,13 @@
11 package namaComment;
22
33 import java.io.BufferedReader;
4+import java.io.DataInputStream;
45 import java.io.DataOutputStream;
56 import java.io.IOException;
67 import java.io.InputStream;
78 import java.io.InputStreamReader;
89 import java.net.Socket;
10+import java.nio.charset.Charset;
911
1012 /**
1113 * ニコ生コメント取得クラス?
@@ -44,43 +46,89 @@
4446
4547 @Override
4648 public final void openSocket() throws Exception{
47- //ソケット繋ぐ。
48- Socket socket = new Socket();
49- socket.setKeepAlive(true);
49+ // コメント鯖のアドレスが取得したとき
50+ if (this.url != null && this.port != 0) {
51+ //ソケット繋ぐ。
52+ Socket socket = new Socket(this.url, this.port);
53+ socket.setKeepAlive(true);
5054
51- // いなたんソースぱくり
52- final String GET_COMMENT_XML = "<thread thread=\"%s\" res_from=\"-%s\" version=\"20061206\" />\0";
53- int start = -1;
54- DataOutputStream msgOutStream = new DataOutputStream(socket.getOutputStream());
55- msgOutStream.writeBytes(String.format(GET_COMMENT_XML, thread, start));
55+ // いなたんソースぱくり
56+ final String GET_COMMENT_XML = "<thread thread=\"%s\" res_from=\"%d\" version=\"20061206\" />\0";
57+ int start = -1;
58+ final String output = String.format(GET_COMMENT_XML, this.thread, start);
59+ DataOutputStream msgOutStream = new DataOutputStream(socket.getOutputStream());
60+ msgOutStream.writeBytes(output);
5661
57- InputStream is = socket.getInputStream();
58- BufferedReader br = new BufferedReader(new InputStreamReader(is));
62+ InputStream is = socket.getInputStream();
63+ BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
5964
60- StringBuilder lines = new StringBuilder();
61- int word = 0;
62- //外ループ
63- while(true){
64- //待機してXMLをもらう
65- String line = br.readLine();
66-// socket.getInputStream().read(b, off, len)
65+ StringBuilder lines = new StringBuilder();
66+ StringBuilder bufs = new StringBuilder();
67+ int word = 0;
68+ String line = "";
6769
68- // これなんだろう?
69- while(word != 0){
70- lines.append((char)word);
71- }
72- // これなんだろう?
70+ //外ループ
71+ while(true){
72+ // 受信メッセージ
73+ lines = new StringBuilder();
74+ // 受信
75+ bufs = new StringBuilder();
76+ char[] buf = new char[16];
77+ br.read(buf, 0, buf.length);
7378
74- //<chat />なら
75- if(true){
76- this.chatRead(lines.toString());
79+ // 区切りがこない間は受信し続ける
80+ String str = String.valueOf(buf);
81+ while (str.indexOf("\0") == -1) {
82+ bufs.append(str);
83+ buf = new char[16];
84+ br.read(buf, 0, buf.length);
85+ str = String.valueOf(buf);
86+ }
87+ bufs.append(str);
88+
89+ // 区切りの左だけ取りだして
90+ line += bufs.toString();
91+ String[] blocks = line.split("\0");
92+ line = blocks.length == 1 ? "" : blocks[1];
93+
94+//debug System.out.println("|" + blocks[0]);
95+ lines.append(blocks[0]+"\0");
96+
97+ // 2つ以上メッセージがあるときは続ける
98+ while (line.indexOf("\0") != -1) {
99+ blocks = line.split("\0");
100+ line = blocks.length == 1 ? "" : blocks[1];
101+
102+//debug System.out.println("|" + blocks[0]);
103+ lines.append(line.split("\0")[0]+"\0");
104+ }
105+
106+ // これなんだろう?
107+// while(word != 0){
108+// lines.append((char)word);
109+// }
110+
111+ line = lines.toString();
112+ while (line.indexOf("\0") != -1) {
113+ String[] tmps = line.split("\0");
114+ String tmp = tmps[0];
115+
116+ //<chat />なら
117+ if (tmp.startsWith("<chat")) {
118+ this.chatRead(tmp);
119+ }
120+ //<thread />なら
121+ if (tmp.startsWith("<thread")) {
122+ this.threadRead(tmp);
123+ }
124+ line = tmps.length == 1 ? "" : tmps[1];
125+ }
126+ //lineを初期化?
77127 }
78- //<thread />なら
79- if(true){
80- this.threadRead(lines.toString());
81- }
82- //lineを初期化?
83128 }
129+ else {
130+ System.out.println("コメントサーバとれてません");
131+ }
84132 }
85133
86134 @Override
--- Java/Nico-Lib-Java-Talisker@ZQN/src/namaComment/sample/NicoLiveCommentSample.java (revision 42)
+++ Java/Nico-Lib-Java-Talisker@ZQN/src/namaComment/sample/NicoLiveCommentSample.java (revision 43)
@@ -1,6 +1,6 @@
11 package namaComment.sample;
22
3-import namaComment.*;;
3+import namaComment.*;
44
55 /**
66 * コメント取得サンプルアプリ
@@ -14,7 +14,7 @@
1414 NamaComment test = new CommentGet("nicolib@bloodpledge.info", "nico-lib");
1515 //みたいな生成でもおk。
1616 //NamaCommennt test = new CommenntGet(”user_session12031289740947");
17- test.openConnection("lv12334982");
17+ test.openConnection("lv12627690");
1818
1919 // TODO このメソッドは同期と考えていいのかな?
2020 test.openSocket();
Show on old repository browser