Phidget22.Connection support for string port

Supporting Browser-based Javascript and Node.js
Post Reply
AdamLee
Phidgetly
Posts: 34
Joined: Wed Jul 17, 2019 9:28 am
Contact:

Phidget22.Connection support for string port

Post 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?
Adam Lee
Software Engineer
www.activatedresearch.com
User avatar
Patrick
Lead Developer
Posts: 3403
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Phidget22.Connection support for string port

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests