forked from hyperledger/web3j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hyperledger#458 from mushketyk/websockets
Websockets support
- Loading branch information
Showing
33 changed files
with
1,806 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
core/src/main/java/org/web3j/protocol/core/methods/response/EthSubscribe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.web3j.protocol.core.methods.response; | ||
|
||
import org.web3j.protocol.core.Response; | ||
|
||
public class EthSubscribe extends Response<String> { | ||
public String getSubscriptionId() { | ||
return getResult(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/java/org/web3j/protocol/core/methods/response/EthUnsubscribe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.web3j.protocol.core.methods.response; | ||
|
||
import org.web3j.protocol.core.Response; | ||
|
||
public class EthUnsubscribe extends Response<Boolean> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
core/src/main/java/org/web3j/protocol/websocket/WebSocketClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.web3j.protocol.websocket; | ||
|
||
import java.net.URI; | ||
|
||
import org.java_websocket.handshake.ServerHandshake; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Web socket client implementation that connects to a specify URI. Allows to provide a listener | ||
* that will be called when a new message is received by the client. | ||
*/ | ||
public class WebSocketClient extends org.java_websocket.client.WebSocketClient { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(WebSocketClient.class); | ||
|
||
private WebSocketListener listener; | ||
|
||
public WebSocketClient(URI serverUri) { | ||
super(serverUri); | ||
} | ||
|
||
@Override | ||
public void onOpen(ServerHandshake serverHandshake) { | ||
log.info("Opened WebSocket connection to {}", uri); | ||
} | ||
|
||
@Override | ||
public void onMessage(String s) { | ||
try { | ||
log.debug("Received message {} from server {}", s, uri); | ||
listener.onMessage(s); | ||
} catch (Exception e) { | ||
log.error("Failed to process message '{}' from server {}", s, uri); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClose(int code, String reason, boolean remote) { | ||
log.info("Closed WebSocket connection to {}, because of reason: '{}'." | ||
+ "Conection closed remotely: {}", uri, reason, remote); | ||
listener.onClose(); | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
log.error(String.format("WebSocket connection to {} failed with error", uri), e); | ||
listener.onError(e); | ||
} | ||
|
||
/** | ||
* Set a listener that will be called when a new message is received by the client. | ||
* | ||
* @param listener WebSocket listener | ||
*/ | ||
public void setListener(WebSocketListener listener) { | ||
this.listener = listener; | ||
} | ||
} |
Oops, something went wrong.