Skip to content

Commit

Permalink
Adds missing semicolons, removes unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
edorgeville committed Oct 26, 2020
1 parent 6ec12b1 commit e74dff1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/transports/mqtt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('mqtt transport', () => {

transport.addRouter(router);

mqttBroker.listen(1883)
mqttBroker.listen(1883);
await transport.connect();
mqttClient = await AsyncMQTT.connectAsync(mqttOptions.broker)
mqttClient.subscribe(mqttOptions.outTopic)
Expand All @@ -36,17 +36,17 @@ describe('mqtt transport', () => {
})

it("can connect to the broker", () => {
expect(transport.client?.connected).toBeTruthy()
expect(transport.client?.connected).toBeTruthy();
})

it("can answer to simple JSON-RPC", (done) => {
const messageHandler = (topic: string, payload: Buffer) => {
const response = JSON.parse(payload.toString())
const response = JSON.parse(payload.toString());
expect(response.result).toBe(4);
mqttClient.off('message', messageHandler)
done()
mqttClient.off('message', messageHandler);
done();
}
mqttClient.on('message', messageHandler)
mqttClient.on('message', messageHandler);

mqttClient.publish(mqttOptions.inTopic, JSON.stringify({
id: "0",
Expand Down
24 changes: 10 additions & 14 deletions src/transports/mqtt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import cors from "cors";
import { json as jsonParser } from "body-parser";
import connect, { HandleFunction } from "connect";
// import http, { ServerOptions } from "http";
import AsyncMQTT, { AsyncClient } from "async-mqtt"
import AsyncMQTT, { AsyncClient } from "async-mqtt";
import ServerTransport, { JSONRPCRequest } from "./server-transport";

export interface MQTTServerTransportOptions {
Expand All @@ -13,25 +9,25 @@ export interface MQTTServerTransportOptions {


export default class MQTTServerTransport extends ServerTransport {
private options: MQTTServerTransportOptions
public client: AsyncMQTT.AsyncClient | null
private options: MQTTServerTransportOptions;
public client: AsyncMQTT.AsyncClient | null;

constructor(options: MQTTServerTransportOptions) {
super()
this.options = { ...options }
this.client = null
super();
this.options = { ...options };
this.client = null;
}

public async connect(): Promise<any> {
this.client = await AsyncMQTT.connectAsync(this.options.broker);
this.client.subscribe(this.options.inTopic)
this.client.subscribe(this.options.inTopic);
this.client.on('message', (topic: string, payload: Buffer) => {
this.mqttRouterHandler(JSON.parse(payload.toString()))
this.mqttRouterHandler(JSON.parse(payload.toString()));
})
}

public end(): void {
this.client?.end()
this.client?.end();
}

private async mqttRouterHandler(payload: any): Promise<void> {
Expand All @@ -41,6 +37,6 @@ export default class MQTTServerTransport extends ServerTransport {
} else {
result = await super.routerHandler(payload);
}
this.client?.publish(this.options.outTopic, JSON.stringify(result))
this.client?.publish(this.options.outTopic, JSON.stringify(result));
}
}

0 comments on commit e74dff1

Please sign in to comment.