Picture of Marko 6403
Registered 2 years 89 days
Marko 6403 Monday, 3 October 2022, 01:52 PM
Service - cannot connect to PbxApi a second time
Hello,

in my (Javascript) app, the client sends a message to the service, which the service forwards to the PBX. The PBX then sends back a response to the service, which the service finally forwards to the client. However, this works only when I launch the client the first time. The second time, the "onconnected" callback is never triggered. Here's the code (of the service-part) which I used to create a minimum reproduction of the problem:

new JsonApi("admin").onconnected(function(conn) {
if (conn.app == "my-app") {
conn.onmessage(function(msg) {
var obj = JSON.parse(msg)
if (obj.mt == "TestMessage") {
var pbxAdminApi = new PbxApi("PbxAdminApi")
pbxAdminApi.onconnected(function(pbxConn) {
// this never happens the 2nd time I launch the app
conn.send(JSON.stringify({ api: "admin", mt: "TestMessageResult", src: obj.src }))
pbxConn.close()
})
}
});
}
});

Could it be that the PbxApi from the previous launch is still open and multiple connections are not allowed?

Edit: sorry for the hard-to-read code snippet. In the preview of the post it looked somewhat better and all indentations were preserved.
Best regards
Marko
Picture of Marko 6403
Registered 2 years 89 days
Marko 6403 Tuesday, 4 October 2022, 10:08 AM
Re: Service - cannot connect to PbxApi a second time
Problem solved: Instead of initializing a new PBX connection every time a new client connects to the service, I now initialize a single global PBX connection which is then shared by all clients.
Andreas Fink
Moderator Registered 12 years 278 days
Andreas Fink (innovaphone) Thursday, 6 October 2022, 06:40 PM
Re: Service - cannot connect to PbxApi a second time
Hello Marko,

i'm sorry for the delay.

The WebSocket connections are handled in the background by the generic app binary, which uses an Api object as a singleton. This is actually what you have already figured out.

An example for handling of the incoming connections can be found here:

https://github.com/innovaphone/jsbadgeexample/blob/main/jsbadgeexample/jsbadgeexample/innovaphone-jsbadgeexampleservice.js

Especially a cleanup in case of the PBX reboot is useful (onclose callback).

Best Regards
Andreas Fink
← You can define your color theme preference here