@@ -16,14 +16,16 @@ public class Crawl extends Thread {
1616 String gid ;
1717 //与弹幕服务器交互的控制器
1818 MessageHandler messageHandler ;
19+ //登录名
20+ String username ;
1921
2022 public Crawl () throws IOException {
2123 rid = Utils .getRoomId ();
2224 }
2325
2426 /**
2527 *
26- * 初始化弹幕服务器地址数据和弹幕分组信息
28+ * 初始化"弹幕服务器地址数据"和"弹幕分组信息"和"登录用户名"
2729 */
2830 public void init () throws IOException {
2931 String ip = Utils .getServerIP ();
@@ -56,6 +58,8 @@ public void init() throws IOException {
5658 }
5759 } else if (msg .startsWith ("type@=setmsggroup" )) {
5860 gid = msg .split ("gid@=" )[1 ].split ("/" )[0 ];
61+ } else if (msg .startsWith ("type@=loginres" )) {
62+ username = msg .split ("username@=" )[1 ].split ("/" )[0 ];
5963 }
6064 }
6165 socket .close ();
@@ -66,43 +70,59 @@ public void login() throws IOException {
6670 System .out .println ("连接弹幕服务器(danmu.douyutv.com:" + ports .get (0 ) + ")" );
6771 messageHandler = new MessageHandler (socket );
6872
69- String loginreq = "type@=loginreq/username@=visitor503535 /password@=1234567890123456/roomid@=" + rid + "/" ;
73+ String loginreq = "type@=loginreq/username@=" + username + " /password@=1234567890123456/roomid@=" + rid + "/" ;
7074 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+
7287 messageHandler .send (joinGroup );
73- System .out .println ("进入" + gid + "号弹幕分组" );
7488 }
7589
7690 @ Override
7791 public void run () {
7892 try {
7993 System .out .println ("房间名:" + Utils .getRoomName ());
8094 System .out .println ("主播:" + Utils .getOwnerName ());
81- if (!Utils .roomIsAlive ()) {
95+ /* if (!Utils.roomIsAlive()) {
8296 System.out.println("房间未开播,程序结束.");
8397 return;
8498 } else {
8599 System.out.println("状态:正在直播");
86- }
100+ }*/
101+ } catch (IOException e ) {
102+ e .printStackTrace ();
103+ }
87104
105+ try {
88106 init ();
89107 login ();
108+ login ();
109+ System .out .println ("--------------------------" );
90110
91111 long start = System .currentTimeMillis ();
92112 while (true ) {
93113 byte [] bytes = messageHandler .read ();
94114 String msg = new String (Arrays .copyOfRange (bytes ,8 ,bytes .length ));
95115
96116 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 ];
99119 System .out .println ("[" + nickname + "]:" + content );
100- // System.out.println(msg);
101120 }
102121
103122 long end = System .currentTimeMillis ();
104123 if (end - start > 30000 ) {
105124 messageHandler .send ("type=mrkl/" );
125+ start = System .currentTimeMillis ();
106126 }
107127
108128 Thread .sleep (1 );
0 commit comments