Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design1
/
RIT_Project1
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
1
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
박권수
2021-05-10 03:26:11 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
8bccae8a608aec4129711c9657e186df2f3b73a6
8bccae8a
1 parent
795b6b2b
feat. mqtt logic going dev
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
9 deletions
server/src/lib/MqttModule.js
server/src/lib/MqttModule.js
View file @
8bccae8
const
mqtt
=
require
(
'mqtt'
);
const
clientList
=
[];
exports
.
mqttOn
=
(
hosting
)
=>
{
const
client
=
mqtt
.
connect
(
hosting
);
client
.
on
(
'connect'
,
()
=>
{
console
.
log
(
'Connected : '
,
client
.
connected
)
exports
.
mqttOn
=
async
(
hosting
)
=>
{
const
filterIndex
=
clientList
.
findIndex
(
client
=>
{
return
(
client
.
options
.
clientId
===
hosting
.
clientId
&&
client
.
options
.
host
===
hosting
.
host
&&
client
.
options
.
port
===
hosting
.
port
)
});
return
client
;
}
if
(
filterIndex
===
-
1
)
{
const
client
=
mqtt
.
connect
(
hosting
);
clientList
.
push
(
client
);
client
.
on
(
'connect'
,
()
=>
{
console
.
log
(
'Client connected: '
,
client
.
connected
);
});
return
client
;
}
else
{
return
clientList
[
filterIndex
];
};
};
exports
.
mqttSubscribe
=
(
client
,
topic
)
=>
{
client
.
subscribe
(
topic
);
client
.
on
(
'message'
,
(
topic
,
message
,
packet
)
=>
{
console
.
log
(
'
topic : '
,
topic
);
console
.
log
(
'
message : '
,
message
.
toString
()
);
console
.
log
(
'
\x1b[1;37mtopic : '
,
topic
,
'\x1b[0m'
);
console
.
log
(
'
\x1b[1;37mmessage : '
,
message
.
toString
(),
'\x1b[0m'
,
'\n'
);
});
}
\ No newline at end of file
};
exports
.
mqttPublishMessage
=
(
client
,
topic
,
message
)
=>
{
client
.
publish
(
topic
,
message
,
()
=>
{});
};
\ No newline at end of file
...
...
Please
register
or
login
to post a comment