Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
device-new-platform
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵啸非
device-new-platform
Commits
da54c2c1
Commit
da54c2c1
authored
May 17, 2022
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改消息组件
parent
c3b17e1e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
11 deletions
+42
-11
device-manager/src/main/java/com/mortals/xhx/daemon/applicationservice/NettyStartedService.java
...ls/xhx/daemon/applicationservice/NettyStartedService.java
+1
-1
device-manager/src/main/java/com/mortals/xhx/daemon/netty/server/controlserver/handler/NettyUDPServerHandler.java
...y/server/controlserver/handler/NettyUDPServerHandler.java
+9
-3
device-manager/src/test/java/com/mortals/httpclient/UDPClientApp.java
...er/src/test/java/com/mortals/httpclient/UDPClientApp.java
+3
-5
device-manager/src/test/java/com/mortals/httpclient/UDPClientHandler.java
...rc/test/java/com/mortals/httpclient/UDPClientHandler.java
+29
-2
No files found.
device-manager/src/main/java/com/mortals/xhx/daemon/applicationservice/NettyStartedService.java
View file @
da54c2c1
...
...
@@ -29,7 +29,7 @@ public class NettyStartedService implements IApplicationStartedService {
public
void
start
()
{
logger
.
info
(
"开始netty服务...."
);
thread
=
new
Thread
(()
->
{
controlServer
.
run
(
7
6
88
);
controlServer
.
run
(
7
7
88
);
});
thread
.
setDaemon
(
true
);
thread
.
start
();
...
...
device-manager/src/main/java/com/mortals/xhx/daemon/netty/server/controlserver/handler/NettyUDPServerHandler.java
View file @
da54c2c1
...
...
@@ -5,6 +5,7 @@ import com.mortals.framework.ap.GlobalSysInfo;
import
com.mortals.xhx.busiz.rsp.ApiResp
;
import
com.mortals.xhx.common.code.YesNoEnum
;
import
com.mortals.xhx.module.device.service.DeviceService
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.Unpooled
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.SimpleChannelInboundHandler
;
...
...
@@ -34,8 +35,13 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
@Override
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
DatagramPacket
msg
)
throws
Exception
{
System
.
out
.
println
(
"111111111"
);
log
.
info
(
"receive->UDP:{}"
,
msg
.
content
().
toString
(
CharsetUtil
.
UTF_8
));
DatagramPacket
packet
=
(
DatagramPacket
)
msg
;
ByteBuf
byteBuf
=
packet
.
copy
().
content
();
byte
[]
bytes
=
new
byte
[
byteBuf
.
readableBytes
()];
byteBuf
.
readBytes
(
bytes
);
String
content
=
new
String
(
bytes
);
log
.
info
(
"receive->UDP:{}"
,
packet
.
sender
().
toString
()
+
","
+
content
);
// TODO 设备返回服务端配置
String
URL
=
GlobalSysInfo
.
getParamValue
(
PARAM_SERVER_HTTP_URL
,
"http://192.168.0.98:11021"
);
...
...
@@ -44,7 +50,7 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
resp
.
setCode
(
YesNoEnum
.
YES
.
getValue
());
resp
.
setMsg
(
"获取服务端地址成功!"
);
resp
.
setData
(
URL
);
ctx
.
writeAndFlush
(
new
DatagramPacket
(
Unpooled
.
copiedBuffer
(
JSON
.
toJSONString
(
resp
),
CharsetUtil
.
UTF_8
),
new
InetSocketAddress
(
7689
),
msg
.
sender
()));
ctx
.
writeAndFlush
(
new
DatagramPacket
(
Unpooled
.
copiedBuffer
(
JSON
.
toJSONString
(
resp
),
CharsetUtil
.
UTF_8
),
msg
.
sender
()));
}
...
...
device-manager/src/test/java/com/mortals/httpclient/UDPClientApp.java
View file @
da54c2c1
...
...
@@ -25,7 +25,7 @@ public class UDPClientApp {
public
void
init
()
throws
IOException
{
try
(
DatagramSocket
sock
=
new
DatagramSocket
())
{
outpack
=
new
DatagramPacket
(
new
byte
[
0
],
0
,
new
InetSocketAddress
(
77
88
));
outpack
=
new
DatagramPacket
(
new
byte
[
0
],
0
,
new
InetSocketAddress
(
InetAddress
.
getByName
(
"192.168.0.1"
),
76
88
));
Scanner
scan
=
new
Scanner
(
System
.
in
);
while
(
scan
.
hasNextLine
())
{
// 键盘输入
byte
[]
buf
=
scan
.
nextLine
().
getBytes
();
// 捕获发送
...
...
@@ -40,21 +40,19 @@ public class UDPClientApp {
public
static
void
main
(
String
[]
args
)
throws
IOException
{
//
new UDPClientApp().init();
//
new UDPClientApp().init();
Bootstrap
bootstrap
=
new
Bootstrap
();
EventLoopGroup
workGroup
=
new
NioEventLoopGroup
();
bootstrap
.
group
(
workGroup
).
channel
(
NioDatagramChannel
.
class
)
.
option
(
ChannelOption
.
SO_BROADCAST
,
true
)
.
handler
(
new
ChannelInitializer
<
NioDatagramChannel
>()
{
@Override
protected
void
initChannel
(
NioDatagramChannel
ch
)
throws
Exception
{
// TODO Auto-generated method stub
ch
.
pipeline
().
addLast
(
new
UDPClientHandler
());
}
});
try
{
Channel
channel
=
bootstrap
.
bind
(
0
).
sync
().
channel
();
Channel
channel
=
bootstrap
.
bind
(
7789
).
sync
().
channel
();
channel
.
closeFuture
().
sync
().
await
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
device-manager/src/test/java/com/mortals/httpclient/UDPClientHandler.java
View file @
da54c2c1
...
...
@@ -2,6 +2,7 @@ package com.mortals.httpclient;
import
java.net.InetSocketAddress
;
import
java.nio.charset.Charset
;
import
java.util.concurrent.TimeUnit
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.Unpooled
;
...
...
@@ -26,8 +27,34 @@ public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket
@Override
public
void
channelActive
(
ChannelHandlerContext
ctx
)
throws
Exception
{
ctx
.
writeAndFlush
(
new
DatagramPacket
(
Unpooled
.
copiedBuffer
(
"hello,server111"
,
Charset
.
forName
(
"UTF-8"
)),
new
InetSocketAddress
(
7688
)));
log
.
info
(
"remoteAddress:{}"
,
ctx
.
channel
().
remoteAddress
());
ctx
.
executor
().
parent
().
execute
(
new
Runnable
()
{
@Override
public
void
run
()
{
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
ctx
.
writeAndFlush
(
new
DatagramPacket
(
Unpooled
.
copiedBuffer
(
"我在广播"
+
i
,
Charset
.
forName
(
"utf-8"
)),
new
InetSocketAddress
(
"255.255.255.255"
,
7788
)));
try
{
TimeUnit
.
SECONDS
.
sleep
(
2
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
});
/* ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("hello,server", Charset.forName("UTF-8")),
new InetSocketAddress(54321)));*/
}
@Override
public
void
channelInactive
(
ChannelHandlerContext
ctx
)
throws
Exception
{
log
.
info
(
"channelInactive remoteAddress:{}"
,
ctx
.
channel
().
remoteAddress
());
}
}
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