Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
live-crawler
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhiwei
live-crawler
Commits
65064349
Commit
65064349
authored
Jan 29, 2019
by
zhiwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
斗鱼弹幕级b站消息队列添加
parent
c472b5c4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
159 additions
and
94 deletions
+159
-94
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageHandler.java
+2
-2
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageListener.java
+13
-0
src/main/java/com/zhiwei/live/danmu/douyu/DouYuClient.java
+0
-0
src/main/java/com/zhiwei/live/danmu/douyu/Main.java
+52
-0
src/main/java/com/zhiwei/live/danmu/douyu/MessageHandler.java
+92
-92
No files found.
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageHandler.java
View file @
65064349
...
@@ -65,8 +65,8 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
...
@@ -65,8 +65,8 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
Matcher
matcher
=
pattern
.
matcher
(
source
);
Matcher
matcher
=
pattern
.
matcher
(
source
);
while
(
matcher
.
find
())
{
while
(
matcher
.
find
())
{
JSONObject
dataJson
=
JSONObject
.
parseObject
(
matcher
.
group
());
JSONObject
dataJson
=
JSONObject
.
parseObject
(
matcher
.
group
());
Bilibili
Entity
bilibiliEntity
=
new
BilibiliEntity
(
dataJson
);
Bilibili
Message
bilibiliMessage
=
new
BilibiliMessage
(
dataJson
);
System
.
out
.
println
(
bilibili
Entity
.
toString
()
);
System
.
out
.
println
(
bilibili
Message
);
}
}
}
}
ReferenceCountUtil
.
release
(
msg
);
ReferenceCountUtil
.
release
(
msg
);
...
...
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageListener.java
0 → 100644
View file @
65064349
package
com
.
zhiwei
.
live
.
danmu
.
bilibili
;
import
java.util.List
;
import
java.util.Vector
;
public
class
BilibiliMessageListener
{
public
static
List
<
BilibiliMessage
>
messages
=
new
Vector
<>();
public
static
void
addMessages
(
BilibiliMessage
bilibiliMessage
)
{
messages
.
add
(
bilibiliMessage
);
}
}
src/main/java/com/zhiwei/live/danmu/douyu/DouYuClient.java
View file @
65064349
This diff is collapsed.
Click to expand it.
src/main/java/com/zhiwei/live/danmu/douyu/Main.java
0 → 100644
View file @
65064349
package
com
.
zhiwei
.
live
.
danmu
.
douyu
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.zhiwei.common.config.GroupType
;
import
com.zhiwei.crawler.proxy.ProxyFactory
;
import
com.zhiwei.live.danmu.douyu.entity.ChatMsg
;
/**
* 功能描述:
*
* @auther: coffee
* @date: 2018-07-04 19:37:41
* 修改日志:
*/
public
class
Main
{
private
final
static
Logger
logger
=
LoggerFactory
.
getLogger
(
Main
.
class
);
private
static
final
String
registry
=
"zookeeper://192.168.0.36:2181"
;
private
static
final
String
group
=
"local"
;
static
{
ProxyFactory
.
init
(
registry
,
group
,
GroupType
.
PROVIDER
);
}
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
DouYuClient
client
=
new
DouYuClient
(
"3084150"
);
client
.
registerMessageListener
(
new
MessageListener
<
ChatMsg
>()
{
@Override
public
void
read
(
ChatMsg
message
)
{
//logger.info(message.toChatStr());
// System.out.println(message.toChatStr());
}
});
client
.
registerMessageListener
(
new
MessageListener
<
String
>()
{
@Override
public
void
read
(
String
message
)
{
System
.
out
.
println
(
message
);
// logger.info(message);
}
});
client
.
login
();
client
.
sync
();
Thread
.
sleep
(
30000
);
client
.
exit
();
}
}
src/main/java/com/zhiwei/live/danmu/douyu/MessageHandler.java
View file @
65064349
package
com
.
zhiwei
.
live
.
danmu
.
douyu
;
package
com
.
zhiwei
.
live
.
danmu
.
douyu
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.net.Socket
;
import
java.net.Socket
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
com.zhiwei.live.danmu.douyu.exceptions.DouYuSDKException
;
import
com.zhiwei.live.danmu.douyu.exceptions.DouYuSDKException
;
/**
/**
* 功能描述:消息处理助手
* 功能描述:消息处理助手
*
*
* @auther: coffee
* @auther: coffee
* @date: 2018-07-04 15:11:13
* @date: 2018-07-04 15:11:13
* 修改日志:
* 修改日志:
*/
*/
public
class
MessageHandler
{
public
class
MessageHandler
{
private
final
static
Logger
logger
=
LoggerFactory
.
getLogger
(
MessageHandler
.
class
);
private
final
static
Logger
logger
=
LoggerFactory
.
getLogger
(
MessageHandler
.
class
);
private
Socket
socket
;
private
Socket
socket
;
public
MessageHandler
(
Socket
socket
)
{
public
MessageHandler
(
Socket
socket
)
{
this
.
socket
=
socket
;
this
.
socket
=
socket
;
}
}
/**
/**
* 发送消息
* 发送消息
*
*
* @param content
* @param content
*/
*/
public
void
send
(
String
content
)
{
public
void
send
(
String
content
)
{
try
{
try
{
Message
message
=
new
Message
(
content
);
Message
message
=
new
Message
(
content
);
OutputStream
out
=
socket
.
getOutputStream
();
OutputStream
out
=
socket
.
getOutputStream
();
out
.
write
(
message
.
getBytes
());
out
.
write
(
message
.
getBytes
());
out
.
flush
();
out
.
flush
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
DouYuSDKException
(
e
);
throw
new
DouYuSDKException
(
e
);
}
}
}
}
/**
/**
* 读取消息
* 读取消息
*
*
* @return
* @return
*/
*/
public
byte
[]
read
(){
public
byte
[]
read
(){
try
{
try
{
InputStream
inputStream
=
socket
.
getInputStream
();
InputStream
inputStream
=
socket
.
getInputStream
();
//下条信息的长度
//下条信息的长度
int
contentLen
=
0
;
int
contentLen
=
0
;
//读取前4个字节,得到数据长度
//读取前4个字节,得到数据长度
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
int
tmp
=
inputStream
.
read
();
int
tmp
=
inputStream
.
read
();
contentLen
+=
tmp
*
Math
.
pow
(
16
,
2
*
i
);
contentLen
+=
tmp
*
Math
.
pow
(
16
,
2
*
i
);
}
}
int
len
=
0
;
int
len
=
0
;
int
readLen
=
0
;
int
readLen
=
0
;
byte
[]
bytes
=
new
byte
[
contentLen
];
byte
[]
bytes
=
new
byte
[
contentLen
];
ByteArrayOutputStream
byteArray
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
byteArray
=
new
ByteArrayOutputStream
();
while
((
len
=
inputStream
.
read
(
bytes
,
0
,
contentLen
-
readLen
))
!=
-
1
)
{
while
((
len
=
inputStream
.
read
(
bytes
,
0
,
contentLen
-
readLen
))
!=
-
1
)
{
byteArray
.
write
(
bytes
,
0
,
len
);
byteArray
.
write
(
bytes
,
0
,
len
);
readLen
+=
len
;
readLen
+=
len
;
if
(
readLen
==
contentLen
)
{
if
(
readLen
==
contentLen
)
{
break
;
break
;
}
}
}
}
return
byteArray
.
toByteArray
();
return
byteArray
.
toByteArray
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
DouYuSDKException
(
e
);
throw
new
DouYuSDKException
(
e
);
}
}
}
}
/**
/**
* 关闭socket通道
* 关闭socket通道
*
*
* @throws IOException
* @throws IOException
*/
*/
public
void
close
(){
public
void
close
(){
try
{
try
{
socket
.
close
();
socket
.
close
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
logger
.
warn
(
"socket通道关闭异常"
,
e
);
logger
.
warn
(
"socket通道关闭异常"
,
e
);
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment