@@ -16,14 +16,16 @@ public class Crawl extends Thread {
16
16
String gid ;
17
17
//与弹幕服务器交互的控制器
18
18
MessageHandler messageHandler ;
19
+ //登录名
20
+ String username ;
19
21
20
22
public Crawl () throws IOException {
21
23
rid = Utils .getRoomId ();
22
24
}
23
25
24
26
/**
25
27
*
26
- * 初始化弹幕服务器地址数据和弹幕分组信息
28
+ * 初始化"弹幕服务器地址数据"和"弹幕分组信息"和"登录用户名"
27
29
*/
28
30
public void init () throws IOException {
29
31
String ip = Utils .getServerIP ();
@@ -56,6 +58,8 @@ public void init() throws IOException {
56
58
}
57
59
} else if (msg .startsWith ("type@=setmsggroup" )) {
58
60
gid = msg .split ("gid@=" )[1 ].split ("/" )[0 ];
61
+ } else if (msg .startsWith ("type@=loginres" )) {
62
+ username = msg .split ("username@=" )[1 ].split ("/" )[0 ];
59
63
}
60
64
}
61
65
socket .close ();
@@ -66,43 +70,59 @@ public void login() throws IOException {
66
70
System .out .println ("连接弹幕服务器(danmu.douyutv.com:" + ports .get (0 ) + ")" );
67
71
messageHandler = new MessageHandler (socket );
68
72
69
- String loginreq = "type@=loginreq/username@=visitor503535 /password@=1234567890123456/roomid@=" + rid + "/" ;
73
+ String loginreq = "type@=loginreq/username@=" + username + " /password@=1234567890123456/roomid@=" + rid + "/" ;
70
74
messageHandler .send (loginreq );
71
- String joinGroup = "type@=joingroup/rid@=" + rid + "/gid@=" + gid + "/" ;
75
+ System .out .println ("登录名:" + username );
76
+
77
+ String joinGroup ;
78
+ if (Utils .isSeaMode ()) {
79
+ joinGroup = "type@=joingroup/rid@=" + rid + "/gid@=-9999/" ;
80
+ System .out .println ("海量弹幕模式:开启" );
81
+ } else {
82
+ joinGroup = "type@=joingroup/rid@=" + rid + "/gid@=" + gid + "/" ;
83
+ System .out .println ("海量弹幕模式:关闭" );
84
+ System .out .println ("进入" + gid + "号弹幕分组" );
85
+ }
86
+
72
87
messageHandler .send (joinGroup );
73
- System .out .println ("进入" + gid + "号弹幕分组" );
74
88
}
75
89
76
90
@ Override
77
91
public void run () {
78
92
try {
79
93
System .out .println ("房间名:" + Utils .getRoomName ());
80
94
System .out .println ("主播:" + Utils .getOwnerName ());
81
- if (!Utils .roomIsAlive ()) {
95
+ /* if (!Utils.roomIsAlive()) {
82
96
System.out.println("房间未开播,程序结束.");
83
97
return;
84
98
} else {
85
99
System.out.println("状态:正在直播");
86
- }
100
+ }*/
101
+ } catch (IOException e ) {
102
+ e .printStackTrace ();
103
+ }
87
104
105
+ try {
88
106
init ();
89
107
login ();
108
+ login ();
109
+ System .out .println ("--------------------------" );
90
110
91
111
long start = System .currentTimeMillis ();
92
112
while (true ) {
93
113
byte [] bytes = messageHandler .read ();
94
114
String msg = new String (Arrays .copyOfRange (bytes ,8 ,bytes .length ));
95
115
96
116
if (msg .startsWith ("type@=chatmessage" )) {
97
- String nickname = msg .split ("@ Snick@A=" )[1 ].split ("@Srg@A" )[0 ];
98
- String content = msg .split ("content@=" )[1 ].split ("/snick@=" )[0 ];
117
+ String nickname = msg .split ("Snick@A=" , 2 )[1 ].split ("@" , 2 )[0 ];
118
+ String content = msg .split ("content@=" , 2 )[1 ].split ("/" , 2 )[0 ];
99
119
System .out .println ("[" + nickname + "]:" + content );
100
- // System.out.println(msg);
101
120
}
102
121
103
122
long end = System .currentTimeMillis ();
104
123
if (end - start > 30000 ) {
105
124
messageHandler .send ("type=mrkl/" );
125
+ start = System .currentTimeMillis ();
106
126
}
107
127
108
128
Thread .sleep (1 );
0 commit comments