I have the below class written in type script. When I compile it, it errors out saying
"src\main\MqttClientWrapper.ts(24,2): error TS1068: Unexpected token. A construct
or, method, accessor, or property was expected."
.
Below is the code I have.
var mqtt :any = require('mqtt');
export interface IWillMessage {
topic: string;
payload: string;
qos: number;
retain: string;
}
export interface IMessageReceivedCallBack {
onMessageReceived(message : string);
}
export interface IMqttOptions {
clientId: string;
keepAlive: number;
clean: string;
reconnectPeriod: string;
will: IWillMessage;
}
export default class MqttClientWrapper {
client : any;
constructor(url: string, mqttOptions : IMqttOptions, messageReceivedCallBack : IMessageReceivedCallBack) {
client = mqtt.connect(url, mqttOptions);
client.on('message',function(topic : string, message : string){
messageReceivedCallBack.onMessageReceived(message);
}
}
subscribeMessage(topic : string) {
client.subscribe(topic);
}
publishMessage(topic : string, message : string, level : number ) {
client.publish(topic,message,level);
}
}
The error is pointing to the line,
client : any;
I have tried " var client :any; " and " let client : any " as well. Still I get the same error. Also below lines of errors are found in the trace..
src\main\MqttClientWrapper.ts(26,16): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,16):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,38): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,38):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,78): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,78):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,106): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,106):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(31,1): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(31,1): e
rror TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,24): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,24):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,34): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,34):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(37,22): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,22):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,40): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,40):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,56): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,56):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,67): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,67):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(41,1): error TS1128: Declaration or statement expe
cted.
Below is the code with the line numbers.
What am I doing wrong here?are those errors also there because of the first "unexpected token error" or something wrong in those lines as well? Please advice.