-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
652 additions
and
25 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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/webank/webase/front/transaction/websocket/SignMessage.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,13 @@ | ||
package com.webank.webase.front.transaction.websocket; | ||
|
||
|
||
import org.fisco.bcos.web3j.protocol.core.Response; | ||
|
||
/** | ||
* eth_sign. | ||
*/ | ||
public class SignMessage extends Response<String> { | ||
public String getSignature() { | ||
return getRawResponse(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/webank/webase/front/transaction/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,58 @@ | ||
package com.webank.webase.front.transaction.websocket; | ||
|
||
|
||
import com.alibaba.fastjson.JSON; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.fisco.bcos.web3j.protocol.websocket.WebSocketListener; | ||
import org.java_websocket.handshake.ServerHandshake; | ||
|
||
import javax.websocket.*; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
public class WebSocketClient extends org.java_websocket.client.WebSocketClient{ | ||
private Optional<WebSocketListener> listenerOpt = Optional.empty(); | ||
|
||
public WebSocketClient(URI serverUri) { | ||
super(serverUri); | ||
} | ||
|
||
@Override | ||
public void onOpen(ServerHandshake serverHandshake) { | ||
log.info("Opened WebSocket connection to {}", uri); | ||
} | ||
|
||
@OnMessage | ||
public void onMessage(String s) { | ||
log.debug("Received message {} from server {}", s, uri); | ||
listenerOpt.ifPresent(listener -> { | ||
try { | ||
listener.onMessage(s); | ||
} catch (Exception e) { | ||
log.error("Failed to process message '{}' from server {}", s, uri, e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onClose(int code, String reason, boolean remote) { | ||
log.info("Closed WebSocket connection to {}, because of reason: '{}'." | ||
+ "Connection closed remotely: {}", uri, reason, remote); | ||
listenerOpt.ifPresent(WebSocketListener::onClose); | ||
} | ||
|
||
@Override | ||
public void onError(Exception ex) { | ||
log.error("WebSocket connection to {} failed with error", uri, ex); | ||
listenerOpt.ifPresent(listener -> listener.onError(ex)); | ||
|
||
} | ||
|
||
public void setListener(WebSocketListener listener) { | ||
this.listenerOpt = Optional.ofNullable(listener); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/webank/webase/front/transaction/websocket/WebSocketRequest.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,26 @@ | ||
package com.webank.webase.front.transaction.websocket; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Objects necessary to process a reply for a request sent via WebSocket protocol. | ||
* | ||
* @param <T> type of a data item that should be returned by the sent request | ||
*/ | ||
public class WebSocketRequest<T> { | ||
private CompletableFuture<T> onReply; | ||
private Class<T> responseType; | ||
|
||
public WebSocketRequest(CompletableFuture<T> onReply, Class<T> responseType) { | ||
this.onReply = onReply; | ||
this.responseType = responseType; | ||
} | ||
|
||
public CompletableFuture<T> getOnReply() { | ||
return onReply; | ||
} | ||
|
||
public Class<T> getResponseType() { | ||
return responseType; | ||
} | ||
} |
Oops, something went wrong.