Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ic4j/ic4j-websocket


https://github.com/ic4j/ic4j-websocket

Last synced: 5 days ago
JSON representation

Awesome Lists containing this project

README

        

## IC4J Websocket Client

This Java library implements the ICP Websocket Gateway protocol, allowing any Java application to receive Websocket messages from ICP smart contracts.


https://github.com/omnia-network/ic-websocket-gateway

# Build

You need JDK 8+ to build IC4J Websocket Agent.

# License

IC4J Websocket is available under Apache License 2.0.

# Documentation

```
KeyPair keyPair = KeyPairGenerator.getInstance("Ed25519").generateKeyPair();

BasicIdentity identity = BasicIdentity.fromKeyPair(keyPair);

ReplicaTransport httpTransport = ReplicaOkHttpTransport.create(env.getProperty("ic.location"));

final WebsocketTransport wsTransport = new JavaxWebsocketTransport(new URI(env.getProperty("ws.location")), httpTransport);

WsAgent wsAgent = new WsAgent( Principal.fromString(env.getProperty("ic.canister")),
wsTransport, identity).setLocal(Boolean.parseBoolean(env.getProperty("local","false")));

wsAgent
.addMessageHandler((output) -> {
try {
AppMessage result = IDLArgs.fromBytes(output).getArgs().get(0).getValue(new PojoDeserializer(), AppMessage.class);
LOG.info(result.text);

AppMessage response = new AppMessage();

response.text = "Pong";

response.timestamp = System.currentTimeMillis();

byte[] payload = WsAgent.encodeValue(response);

wsAgent.sendApplicationMessage(payload);
} catch (Exception e) {
LOG.error(e.getLocalizedMessage(),e);
}
});

wsAgent.start();

// wait 100 seconds for messages from websocket
Thread.sleep(100000);

wsAgent.close();
```

# Downloads / Accessing Binaries

To add IC4J Websocket library to your Java project use Maven or Gradle import from Maven Central.


https://search.maven.org/artifact/org.ic4j/ic4j-websocket/0.7.0/jar

```

org.ic4j
ic4j-websocket
0.7.0

```

```
implementation 'org.ic4j:ic4j-websocket:0.7.0'
```