Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java-demo
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
open-source
java-demo
Commits
7a4f80b4
Commit
7a4f80b4
authored
May 28, 2019
by
杜祥龙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix 记录消息格式
Signed-off-by: sdvdxl杜龙少 <sdvdxl@163.com>
parent
e86f1cc0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
6 deletions
+10
-6
src/main/java/me/hekr/demo/GatewayDevice.java
+2
-3
src/main/java/me/hekr/demo/TcpClient.java
+8
-3
No files found.
src/main/java/me/hekr/demo/GatewayDevice.java
View file @
7a4f80b4
...
@@ -45,7 +45,6 @@ public class GatewayDevice {
...
@@ -45,7 +45,6 @@ public class GatewayDevice {
message
.
put
(
"params"
,
params
);
message
.
put
(
"params"
,
params
);
tcpClient
.
send
(
JSON
.
toJSONString
(
message
)
+
"\n"
);
tcpClient
.
send
(
JSON
.
toJSONString
(
message
)
+
"\n"
);
String
respMsg
=
tcpClient
.
receive
(
MAX_WAIT_TIME_MILLS
);
String
respMsg
=
tcpClient
.
receive
(
MAX_WAIT_TIME_MILLS
);
System
.
out
.
println
(
respMsg
);
JSONObject
respMap
=
JSON
.
parseObject
(
respMsg
);
JSONObject
respMap
=
JSON
.
parseObject
(
respMsg
);
boolean
success
=
respMap
.
getIntValue
(
"code"
)
==
200
;
boolean
success
=
respMap
.
getIntValue
(
"code"
)
==
200
;
if
(!
success
)
{
if
(!
success
)
{
...
@@ -72,7 +71,6 @@ public class GatewayDevice {
...
@@ -72,7 +71,6 @@ public class GatewayDevice {
message
.
put
(
"params"
,
params
);
message
.
put
(
"params"
,
params
);
tcpClient
.
send
(
JSON
.
toJSONString
(
message
)
+
"\n"
);
tcpClient
.
send
(
JSON
.
toJSONString
(
message
)
+
"\n"
);
String
respMsg
=
tcpClient
.
receive
(
MAX_WAIT_TIME_MILLS
);
String
respMsg
=
tcpClient
.
receive
(
MAX_WAIT_TIME_MILLS
);
System
.
out
.
println
(
respMsg
);
JSONObject
resp
=
JSON
.
parseObject
(
respMsg
);
JSONObject
resp
=
JSON
.
parseObject
(
respMsg
);
if
(
resp
.
getIntValue
(
"code"
)
!=
200
)
{
if
(
resp
.
getIntValue
(
"code"
)
!=
200
)
{
System
.
out
.
println
(
"gateway login failed,resp:"
+
respMsg
);
System
.
out
.
println
(
"gateway login failed,resp:"
+
respMsg
);
...
@@ -120,7 +118,8 @@ public class GatewayDevice {
...
@@ -120,7 +118,8 @@ public class GatewayDevice {
new
MessageListener
()
{
new
MessageListener
()
{
@Override
@Override
public
void
onMessage
(
String
message
)
{
public
void
onMessage
(
String
message
)
{
System
.
out
.
println
(
message
);
// TODO 指令处理
System
.
out
.
print
(
"ON_MESSAGE_TODO:"
+
message
);
}
}
}));
}));
}
}
...
...
src/main/java/me/hekr/demo/TcpClient.java
View file @
7a4f80b4
...
@@ -61,7 +61,10 @@ public class TcpClient {
...
@@ -61,7 +61,10 @@ public class TcpClient {
throw
new
RuntimeException
(
"output channel closed"
);
throw
new
RuntimeException
(
"output channel closed"
);
}
}
System
.
out
.
println
(
"SEND => "
+
message
);
System
.
out
.
print
(
"SEND ==> "
+
message
);
if
(!
message
.
endsWith
(
"\n"
)){
System
.
out
.
println
();
}
outToServer
.
writeBytes
(
message
);
outToServer
.
writeBytes
(
message
);
return
this
;
return
this
;
}
}
...
@@ -96,7 +99,9 @@ public class TcpClient {
...
@@ -96,7 +99,9 @@ public class TcpClient {
* @throws IOException
* @throws IOException
*/
*/
public
String
receive
()
throws
IOException
{
public
String
receive
()
throws
IOException
{
return
inFromServer
.
readLine
();
String
msg
=
inFromServer
.
readLine
();
System
.
out
.
println
(
"RECE <== "
+
msg
+
"\n"
);
return
msg
;
}
}
/**
/**
...
@@ -107,7 +112,7 @@ public class TcpClient {
...
@@ -107,7 +112,7 @@ public class TcpClient {
*/
*/
public
String
receive
(
int
timeoutMill
)
public
String
receive
(
int
timeoutMill
)
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
FutureTask
<
String
>
futureTask
=
new
FutureTask
<>(
()
->
inFromServer
.
readLine
()
);
FutureTask
<
String
>
futureTask
=
new
FutureTask
<>(
this
::
receive
);
futureTask
.
run
();
futureTask
.
run
();
return
futureTask
.
get
(
timeoutMill
,
TimeUnit
.
MILLISECONDS
);
return
futureTask
.
get
(
timeoutMill
,
TimeUnit
.
MILLISECONDS
);
}
}
...
...
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