This is a template project which combines HTTP and WebSocket clients together.
It can be used to implement a typical scenario where an init-step is done via HTTP (e.g. Login) and then the data from this step (e.g. cookies) is used to establish a WebSocket connection. When WebSocket connection dies or if init-step
fails, this sequence will be performed again if client itself is in started-state. This behavior is implemented via hybrid-handler which is shared between HTTP and WebSocket clients where such init-step can be implemented in preOpen
method.
The main configuration is done by changing following properties:
- autoStart - start service automatically (
true
by default and ifstartControl
isfalse
) - autoStopAfter - stop after N seconds if the service was started automatically prior to send (
0
by default which means disabled) - sessionGroup - session group for incoming/outgoing th2 messages (equal to session alias by default)
- maxBatchSize - max size of outgoing message batch (
100
by default) - maxFlushTime - max message batch flush time (
1000
by default) - useTransport - use th2 transport or protobuf protocol to publish incoming/outgoing messages (
true
by default) - grpcStartControl - enables start/stop control via gRPC service (
false
by default) - http - HTTP connection configuration block
- ws - WebSocket connection configuration block
- https - enables HTTPS (
false
by default) - host - host for HTTP requests (e.g.
google.com
) - port - port for HTTP requests (
80
by default or443
ifhttps
=true
) - readTimeout - socket read timeout in ms (
5000
by default) - keepAliveTimeout - socket inactivity timeout in ms (
15000
by default) - defaultHeaders - map of default headers, and their values which will be applied to each request (existing headers are not affected, empty by default)
- sessionAlias - session alias for incoming/outgoing HTTP messages (e.g.
rest_api
)
- uri - connection URI
- frameType - outgoing WebSocket frame type, can be either
TEXT
orBINARY
(TEXT
by default) - pingInterval - interval for sending ping-messages in ms (
30000
by default) - sessionAlias - session alias for incoming/outgoing WS messages (e.g.
ws_api
)
Service will also automatically connect prior to message send if it wasn't connected
autoStart: true
autoStopAfter: 0
grpcStartControl: false
sessionGroup: session-group
maxBatchSize: 100
maxFlushTime: 1000
useTransport: true
ws:
uri: wss://echo.websocket.org
frameType: TEXT
pingInterval: 30000
sessionAlias: api_session_ws
http:
https: false
host: someapi.com
port: 334
sessionAlias: api_session_http
readTimeout: 5000
keepAliveTimeout: 15000
defaultHeaders: [ ]
- input pin with
subscribe
,http_send
,group
attributes for consuming HTTP requires via protobuf protocol - input pin with
subscribe
,http_send
,transport-group
attributes for consuming HTTP requires via th2 transport protocol - input pin with
subscribe
,ws_send
,group
attributes for consuming WebSocket messages via protobuf protocol - input pin with
subscribe
,ws_send
,transport-group
attributes for consuming WebSocket messages via th2 transport protocol - output pin with
publish
,raw
for sent HTTP and WebSocket requests / responses - output pin with
publish
,transport-group
for sent HTTP and WebSocket requests / responses
This section describes the messages received and produced by the service
This service receives messages that will be sent via WS as MessageGroup
s, containing a single RawMessage
with a message body
This service receives HTTP requests as MessageGroup
s containing one of:
- a single
RawMessage
containing request body, which can haveuri
,method
, andcontentType
properties in its metadata, which will be used in resulting request - a single
Message
withRequest
message type containing HTTP request line and headers and aRawMessage
described above
If both Message
and RawMessage
contain uri
, method
, and contentType
, values from Message
take precedence.
If none of them contain these values /
and GET
will be used as uri
and method
values respectively
Incoming and outgoing messages are sent via MQ as MessageGroup
s, containing a single RawMessage
with a message body.
Here's an example of infra-mgr
config required to deploy this service
apiVersion: th2.exactpro.com/v1
kind: Th2Box
metadata:
name: ws-client
spec:
imageName: ghcr.io/th2-net/th2-conn-http-ws-client
imageVersion: 0.0.2
customConfig:
autoStart: true
autoStopAfter: 0
grpcStartControl: false
sessionGroup: session-group
maxBatchSize: 100
maxFlushTime: 1000
useTransport: true
ws:
uri: wss://echo.websocket.org
frameType: TEXT
pingInterval: 30000
sessionAlias: api_session_ws
http:
https: false
host: someapi.com
port: 334
sessionAlias: api_session_http
readTimeout: 5000
keepAliveTimeout: 15000
defaultHeaders: [ ]
type: th2-conn
pins:
mq:
subscribers:
- name: to_send_http_proto
attributes:
- subscribe
- http_send
- group
- name: to_send_ws_proto
attributes:
- subscribe
- ws_send
- group
- name: to_send_http_transport
attributes:
- subscribe
- http_send
- transport-group
- name: to_send_ws_transport
attributes:
- subscribe
- ws_send
- transport-group
publishers:
- name: outgoing_proto
attributes:
- publish
- raw
- name: outgoing_transport
attributes:
- publish
- transport-group