Command Line Options
Websocketserver is a PyQt application that can be run with the ADR supplied cpython interpreter. The resulting command line looks like this:Websocketserver includes a reservation and launching mechanism that can support local, preconfigured services such as EnSight and EnVision or more complex, scripted operations via custom service configurations. A specific example of local services is illustrated in the Builtin Service Operation section. More complex operations can be configured using the –remote_session_config mechanism outlined in the External Services section. The server can also provide HTTP based services if the –http_directory option is specified. If it is, the files in the directory specified by that option will be served up via HTTP on the port specified by –http_port. Enabling the HTTP server also enables a REST API that can be used to interact with the server via HTTP GET requests and a /ansys redirect HTTP Server which simplifies the deployment of tools that leverage the Ansys ADR 3D viewer web component.
The timeout option specifies that a reservation that is made, but left unused for some number of seconds may be reclaimed by the system (e.g. the user reserved a service instance but did not activate it while viewing a web page containing the reservation token). The default timeout is 5min (300s) if left unspecified.
Basic Operation
Websocketserver will route a TCP/IP stream from a listening socket to a client via the ws or wss protocol. Usually the client is a JavaScript application running in a browser. For example, one could tunnel the EnSight vnc protocol to a viewer in a webpage like this:-
Launch EnSight with an embedded vnc service listening for connections on port 5900
-
Launch websocketserver serving up some web pages (the JavaScript VNC client) on port 9000. The local host port 5900 should be served up via ws:// on port 9090
-
In a web browser, browse to this URL
The browser-based VNC client will connect to the running EnSight instance and display the remote imagery. Websocketserver includes REST API to control its operation.
-
The server may be stopped by browsing to the URL:
Builtin Service Operation
Websocketserver allows for some pre-configured services, including EnSight and EnVision to be used. This can be used to support 3D remote graphics using EnVision like this:-
Launch websocketserver serving up some web pages (http) on port 28000 and the web socket protocol on port 28001:
-
In a web browser, browse to this URL:
External Services
Websocketserver provides a mechanism for to launch an application in response to a ws/wss request. The application being launched must provide the host and port number of a listening socket that websocketserver will connect to and tunnel via the ws/wss protocol to the caller. Each unique launching mechanism is referred to as a “service”. Individual services may serve up multiple potential socket connections via websocketserver. A service instance may require significant system resources, so a REST-based service reservation/allocation system is built into websocketserver.Before a service can be instantiated, it must first be reserved. The reservation process marks the service as having been allocated, usually for use in a single web page. The reservation process associates a “token” with the potential service instance. Tokens are generally GUIDs, but should be treated as short strings that do not include any whitespace. A reservation may be requested via the REST API interface. An allocated token is returned as the result of a successful reservation. Normally, an application would embed this token into the HTML/JavaScript of a web page. When an incoming ws/wss request is make with the aforementioned token, websocketserver will launch the hosting application. It will then connect the listening socket created during the launching process to the incoming ws/wss connection. When the w/wss connection is lost, websocketserver will arrange for the launched application to be stopped. In the Basic Operation example where the socket being forwarded had previously been established, only the hostname and port of the websocketserver web socket service are included in the URL. For an external web service to be started by websocketserver, the allocated token must be passed in the URL that is used to begin the websocket stream so websocketserver knows what to launch. Expanding on the example above, the path of “websockify” is passed and the “token” passed in the query portion of the URL:
Service Configuration
Services are described to websocketserver via a service config file. This is a JSON format file provided by the end-user. It includes a list of “services” and a link to a Python script that encapsulates the startup and shutdown of a service instance. The script is responsible for initiating and terminating a socket connection that the websocketserver engine will make available via the websocket protocol using a URL containing the service specific token. An example might look like this:Each key is the name of a type of service. The value of each key is another object.
There are three required keys in the object:
- script : the name of a Python script file responsible for creating an object that can start and stop a service.
- count : the maximum number of simultaneous instances of this service type
- options : a dictionary of optional data items that can be used by ADR to customize the web interface and any additional data items that can be used by the session instance
Internal Services
In addition to the external services specified in the JSON file specified by the –remote_session_config option, websocketserver support the notation of internal, built-in services. Two such services are currently defined: ‘ensight’ and ‘envision’. The –local_session command line option specifies the number of each of these services websocketserver will make available. For example: –local_session ensight 2 will make two instances of EnSight available. The local session is created by launching the associated application on the local computer and the vnc server will be made available over the websocket protocol as per the external services described previously.HTTP Server
If –http_port is provided on the command line, websocketserver will start an HTTP server on that port that will serve up readonly copies of the file tree rooted at the directory pointed to by –http_directory. It will allow the user to browse the files and can be used to deliver additional web content. Two URL namespaces are reserved by the server. The /v1/ root pathname (and future /v{x}/ pathnames) are reserved for the REST API. The /ansys/ root pathname is reserved for the purpose of serving other Ansys services.Ansys ADR Viewer Service
If the GET pathname starts with /ansys/, then the directory content served up will be the ADR /ansys directory. For example, if one specified –http_directory as /tmp, thenhttp://{host}:{port}/tmp/foo would return the file /tmp/foo to the caller. The URL http://{host}:{port}/ansys251/nexus/viewer-loader.js, then the file returned will be {install_root}/ansys251/nexus/viewer-loader.js. This makes it easy to start a 3D viewer service via webcomponentviewer_api.
EnSight REST API Service
Starting with 2024 R1, EnSight now supports a REST API. Websocketserver can be configured to serve up this API as well. To do this, websocketserver needs to be able to create a gRPC connection to the EnSight instance. The –grpc_port command line option is used for this. It is assumed the host is the same as –client_hostname. Note that one must provide a security key in this case. That key will be used as the sessionid for the REST API.REST API
Websocketserver includes a REST API that allows an external application (ADR) to stop the server, reserve a session and view the core status of the services layer. If the incoming GET pathname starts with /v1/, then the GET request is interpreted as being a REST API call. The current API is implemented using REST GET operations on the websocketserver HTML server port (–http_port). Note that –http_directory must be specified for the HTML server to be enabled. The API namespace is currently defined as:/v1/stop- shutdown the server/v1/status- return the status of the current services as a JSON formatted string/v1/status_view- return the status of the current services as an HTML table (human readable)/v1/reserve/{servicename}?option1=value1&option2=value2- reserve a service with the specified options/v1/release/{token}- release a reserved or active token