I have been attempting to integrate the vertx-eventbus-client.js 3.8.3 into my Angular web project with some success. Initially, the following code worked perfectly:
declare const EventBus: any;
@Injectable({
providedIn: 'root'
})
export class DeviceService implements OnInit {
constructor(private httpClient: HttpClient) {
var eb = new EventBus("http://127.0.0.1:8080/eventbus");
eb.onopen = function() {
eb.registerHandler("events-feed", function(error, message) {
console.log("Message: " + message.body);
});
eb.registerHandler("receiver", function(error, message) {
console.log('Message: ' + message.body);
});
eb.publish("events-feed", "Test", function (error, message) {
console.log(JSON.parse(message.body));
});
}
}
}
The connection is successfully established with the backend and I am able to receive messages through the registerHandler method. However, when I try to move this code within a function like so:
private eb : any;
initializeEventBus() {
this.eb = new EventBus("http://127.0.0.1:8080/eventbus");
this.eb.onopen = function() {
this.eb.registerHandler("events-feed", function(error, message) {
console.log("Message: " + message.body);
});
this.eb.registerHandler("receiver", function(error, message) {
console.log('Message: ' + message.body);
});
this.eb.publish("events-feed", "Test", function (error, message) {
console.log(JSON.parse(message.body));
});
}
}
An error occurs and I receive the following error message:
core.js:14597 ERROR TypeError: Cannot read property 'registerHandler' of undefined
at EventBus.DeviceService.eb.onopen (device.service.ts:28)
at w.self.sockJSConn.onopen (vertx-eventbus.js:121)
at w.r.dispatchEvent (eventtarget.js:51)
at w._open (main.js:320)
at w._transportMessage (main.js:253)
at u.i.emit (emitter.js:50)
at WebSocket.ws.onmessage [as __zone_symbol__ON_PROPERTYmessage] (websocket.js:35)
at WebSocket.wrapFn (zontext.js:1332)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:16147)
Any insights on what may be going wrong here would be greatly appreciated.
Best regards