https://github.com/engineering-research-and-development/true-connector-websocket_message_streamer
https://github.com/engineering-research-and-development/true-connector-websocket_message_streamer
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/engineering-research-and-development/true-connector-websocket_message_streamer
- Owner: Engineering-Research-and-Development
- Created: 2020-05-22T13:03:41.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-07-09T14:02:30.000Z (11 months ago)
- Last Synced: 2025-01-27T21:24:45.444Z (4 months ago)
- Language: Java
- Size: 77.1 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# WebSocket Message Streamer
## Installation
Clone the repo and launch `mvn install`In a maven project include this dependency:
```it.eng.idsa
websocket-message-streamer
1.0-SNAPSHOT```
## Usage
Copy config.properties file to resource folder of application which is using this library.
Content of the *config.properties* file:
```
### App setup
server.ssl.key-password=changeit
server.ssl.key-store=/cert/ssl-server.jks
server.ssl.port=9000
server.path=/incoming-data-channel-received-message
# Timer in ms
application.recreation.frequency=1000
```
Modify values for keystore password and location of jks file, according your configuration.In the Bootstrap of your application `init` the library in this way (**Spring Boot** not required):
```
@Configuration
public class WebSockeMessageStreamerConfig {@Bean
public FileRecreatorBeanExecutor fileRecreatorBeanTimer() throws SchedulerException {
FileRecreatorBeanExecutor fileRecreatorBeanExecutor = WebSocketServerManager.fileRecreatorBeanExecutor();
fileRecreatorBeanExecutor.setPort(9060); //optional default 9000
fileRecreatorBeanExecutor.setKeystorePassword("server.jks"); //optional default classpath: ssl-server.jks
fileRecreatorBeanExecutor.setKeystorePassword("password");
fileRecreatorBeanExecutor.trigger();
return fileRecreatorBeanExecutor;
}@Bean
public IncomingDataAppResourceOverWs incomingDataAppResourceOverWs(){
IncomingDataAppResourceOverWs incomingDataAppResourceOverWs = new IncomingDataAppResourceOverWs();
WebSocketServerManager.messageWebSocketResponse().addPropertyChangeListener(incomingDataAppResourceOverWs);
return incomingDataAppResourceOverWs;
}
}```
Example of class to manage the `Response` arrived from Server:```
public class IncomingDataAppResourceOverWs implements PropertyChangeListener {private String responseMessage;
public String getResponseMessage() {
return responseMessage;
}public void setResponseMessage(String responseMessage) {
this.responseMessage = responseMessage;
}@Override
public void propertyChange(PropertyChangeEvent evt) {
this.setResponseMessage((String) evt.getNewValue());
WebSocketServerManager.messageWebSocketResponse().sendResponse(createDummyResponse(getResponseMessage()));
}private String createDummyResponse(String responseMessage) {
String responseString = null;
try {
String header = "A simple Header";
// Put check sum in the payload
String payload = "{\"checksum\":\"ABC123\"}";
// prepare multipart message.
responseString = new MultiPartMessage.Builder()
.setHeader(header)
.setPayload(payload)
.build()
.toString();
} catch (Exception e) {
e.printStackTrace();
}
return responseString;
}
}```
In A Rest Api or wherever you need, use the WS Client in this way:
```
@PostMapping("/sendFile")
@ResponseBody
public String sendFile(@RequestHeader("Forward-To") String forwardTo, @RequestBody String fileName) throws Exception {
Resource resource = resourceLoader.getResource("classpath:examples-multipart-messages/" + fileName);
return WebSocketClientManager.messageWebSocketSender().sendMultipartMessageWebSocketOverHttps(resource.getFile(), forwardTo);
}```
`forward-To` is the address of a WS Server.