mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 18:43:10 +08:00
20 lines
412 B
TypeScript
20 lines
412 B
TypeScript
import { connect } from "mqtt";
|
|
|
|
var client = connect('ws://' + location.host + '/mqtt',
|
|
{
|
|
clientId: "client1",
|
|
});
|
|
|
|
client.on('connect', () => {
|
|
client.subscribe('presence');
|
|
client.publish('presence', 'Hello mqtt');
|
|
});
|
|
|
|
client.on('message', (topic, message) => {
|
|
// message is Buffer
|
|
console.log(message.toString());
|
|
});
|
|
|
|
window.onbeforeunload = () => {
|
|
client.end();
|
|
}; |