Page 1 of 1

Phidget22.Connection support for string port

Posted: Tue Jul 30, 2019 7:55 am
by AdamLee
I'm migrating hard-coded values into environment variables in my project, so naturally the Phidget Network Server port is a good candidate. Unfortunately it looks like Phidget22.Connection doesn't support strings as part of the constructor so I have to convert it to a number. Can the connection class be changed to better support environment variables for port number?

What I want to do:

let connection = new Phidget22.Connection(process.env.PHIDGET_NETWORK_SERVER_PORT, 'localhost');

What I have to do:
let connection = new Phidget22.Connection(parseInt(process.env.PHIDGET_NETWORK_SERVER_PORT, 10), 'localhost');

Also just so I'm clear - is this the correct place to report bugs and feature requests for language specific APIs?

Re: Phidget22.Connection support for string port

Posted: Wed Jul 31, 2019 2:55 pm
by Patrick
Hi,

The Connection constructor operates in 3 modes. When the first argument is a string, this is interpreted as a URI string.

I'd suggest using the first form of the constructor, which takes an optional object (see the API docs). In this object, the port can be specified as either a string or a number, or left out entirely to use the default port.

Code: Select all

let connection = new Phidget22.Connection({
	port: process.env.PHIDGET_NETWORK_SERVER_PORT, 
	hostname: 'localhost'
});
You can also omit hostname to use the default, which is 'localhost', or entirely omit the options object to use default hostname and port.

Code: Select all

let connection = new Phidget22.Connection();
-Patrick