アカウント イベントのサブスクライブ
関数プロトタイプ
public BigInteger listenAccount(Identity id, IEventCallback handler, EventModelType type)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
id | true | Identity | アカウントのビジネス ID です。 |
handler | true | IEventCallback | 応答を受信した場合、タイムアウトが発生した場合、またはエラーが発生した場合にコールバックを実行します。 |
type | true | EventModelType | イベントモデルのタイプ。1 は PULL、2 は PUSH を示します。 |
レスポンス フィールド
レスポンス フィールド | フィールド タイプ | 説明 |
eventId | BigInteger | 返された結果。 eventId が 0 の場合、リスニングは失敗しました。 |
例
// イベント ハンドラ
IEventCallback handler = new IEventCallback() {
@Override
public void onEvent(Message message) {
PushAccountEvent accountEvent = (PushAccountEvent) message;
//コード
}
};
// アカウント: Tester001
Identity identity = new Identity("c60a9d48105950a0cca07a4c6320b98c303ad42d694a634529e8e1a0a16fcdb5");
// アカウントをリスニングする
BigInteger eventId = sdk.getEventService().listenAccount(identity, handler, EventModelType.PUSH)
if (eventId.longValue() == 0) {
System.out.println("リスニングに失敗しました");
}
コントラクト イベントのサブスクライブ
関数プロトタイプ
public BigInteger listenContract(Identity id, IEventCallback handler, EventModelType type)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
id | true | Identity | アカウントのビジネス ID です。 |
handler | true | IEventCallback | 応答を受信した場合、タイムアウトが発生した場合、またはエラーが発生した場合にコールバックを実行します。 |
type | true | EventModelType | イベントモデルのタイプ。 1 は PULL、2 は PUSH を示します。 |
レスポンス フィールド
レスポンス フィールド | フィールド タイプ | 説明 |
result | BigInteger | 返された結果。 result が 0 の場合、リスニングは失敗しました。 |
例
/イベント ハンドラ
IEventCallback handler = new IEventCallback() {
@Override
public void onEvent(Message message) {
PushContractEvent eventContractMessage = (PushContractEvent) message;
// コード
}
};
Identity identity = new Identity("a7937b64b8caa58f03721bb6bacf5c78cb235febe0e70b1b84cd99541461a08e");
// コントラクトをリスニングする
BigInteger result = sdk.getEventService().listenContract(identity, handler, EventModelType.PUSH)
トピック イベントのサブスクライブ
関数プロトタイプ
public BigInteger listenTopics(List<String> topics, IEventCallback handler, VMTypeEnum vmType, EventModelType type)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
topics | true | List | サブスクライブするトピックのリスト。 |
handler | true | IEventCallback | 応答を受信した場合、タイムアウトが発生した場合、またはエラーが発生した場合にコールバックを実行します。 |
vmType | true | VMTypeEnum | コントラクトのタイプ。 1 は EVM、2 は WASM を示します。 |
type | true | EventModelType | イベントモデルのタイプ。 1 は PULL、2 は PUSH を示します。 |
レスポンス フィールド
レスポンス フィールド | フィールド タイプ | 説明 |
result | BigInteger | 返された結果。 result が 0 の場合、リスニングは失敗しました。 |
EVM の例
//EVM コントラクト
contract logAndPrecomileContract {
event TEST(identity id, uint a, uint b);
...
function test(identity id, uint ba) public payable returns(uint) {
if (!id.send(ba / 2))
throw; // Sharer への転送も元に戻します
emit TEST(id, ba, id.balance);
return id.balance;
}
...
}
//EVM コントラクトをデプロイする
// イベント ハンドラ
IEventCallback handler = new IEventCallback() {
@Override
public void onEvent(Message message) {
PushTopicsEvent eventTopicsMessage = (PushTopicsEvent) message;
//コード
}
};
// コントラクトを追加する
List<String> topics = new ArrayList<>();
// システム コントラクト。任意のコントラクトが呼び出されると、イベントが返されます。
topics.add("call_contract");
// EVM コントラクト。コントラクトを呼び出すと、コントラクトの内部 TEST イベントがトリガーされます。
IHash hashTool = HashFactory.getHash();
byte[] sha256 = hashTool.hash("TEST(identity,uint256,uint256)".getBytes());
topics.add(ByteUtils.toHexString(sha256));
// トピックをリスニングする
BigInteger result = sdk.getEventService().listenTopics(topics, handler, VMTypeEnum.EVM, EventModelType.PUSH)
// EVM コントラクトを呼び出すと、call_contract と TEST の 2 つのトピック イベントがトリガーされます。
EVMParameter parameters = new EVMParameter("test(identity,uint256)");
parameters.addIdentity(identity);
parameters.addUint(value);
CallContractRequest request = new CallContractRequest(accId, contractId, parameters, BigInteger.ZERO, VMTypeEnum.EVM);
sdk.getContractService().callContract(request);
// トピックのサブスクライブを解除する。
sdk.getEventService().unListenTopics(result);
WASM の例
//Wasm コントラクト
class logAndPrecomileContract : public Contract {
public:
INTERFACE uint64_t test(const Identity& id, uint64_t ba){
uint64_t balance = 0;
bool ret = GetBalance(id, balance);
Log(id, {"hello", "abc"});
return balance;
}
}
//WASM コントラクトをデプロイする
// イベント ハンドラ
IEventCallback handler = new IEventCallback() {
@Override
public void onEvent(Message message) {
PushTopicsEvent eventTopicsMessage = (PushTopicsEvent) message;
//コード
}
};
// コントラクトを追加する
List<String> topics = new ArrayList<>();
// システム コントラクト。任意のコントラクトが呼び出されると、イベントが返されます。
topics.add("call_contract");
topics.add("hello"); // hello イベントをリスニングする。
topics.add("abc"); // abc イベントをリスニングする。
// トピックをリスニングする
BigInteger result = sdk.getEventService().listenTopics(topics, handler, VMTypeEnum.WASM, EventModelType.PUSH)
// WASM コントラクトを呼び出すと、call_contract がトリガーされます。
WASMParameter parameters = new WASMParameter("test");
parameters.addIdentity(identity);
parameters.addUInt64(value);
CallContractRequest request = new CallContractRequest(accId, contractId, parameters, BigInteger.ZERO, VMTypeEnum.WASM);
sdk.getContractService().callContract(request);
// トピックのサブスクライブを解除する。
sdk.getEventService().unListenTopics(result);
ブロック イベントのサブスクライブ
関数プロトタイプ
public BigInteger listenBlock(IEventCallback handler, EventModelType type)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
handler | true | IEventCallback | 応答を受信した場合、タイムアウトが発生した場合、またはエラーが発生した場合にコールバックを実行します。 |
type | true | EventModelType | イベントモデルのタイプ。 1 は PULL、2 は PUSH を示します。 |
レスポンス フィールド
レスポンス フィールド | フィールド タイプ | 説明 |
result | BigInteger | 返された結果。 result が 0 の場合、リスニングは失敗しました。 |
例
// イベント ハンドラ
IEventCallback handler = new IEventCallback() {
@Override
public void onEvent(Message message) {
// コード
}
};
// ブロックをリスニングする
BigInteger result = sdk.getEventService().listenBlock(handler, EventModelType.PUSH)
アカウント イベントのサブスクライブ解除
関数プロトタイプ
public boolean unListenAccount(BigInteger eventId)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
eventId | true | BigInteger | サブスクライブを解除するイベントの ID です。 |
コントラクト イベントのサブスクライブ解除
関数プロトタイプ
public boolean unListenContract(BigInteger eventId)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
eventId | true | BigInteger | サブスクライブを解除するイベントの ID です。 |
トピック イベントのサブスクライブ解除
関数プロトタイプ
public boolean unListenTopics(BigInteger eventId)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
eventId | true | BigInteger | サブスクライブを解除するイベントの ID です。 |
ブロック イベントのサブスクライブ解除
関数プロトタイプ
public boolean unListenBlock(BigInteger eventId)
リクエスト パラメーター
パラメーター | 必須 | タイプ | 説明 |
eventId | true | BigInteger | サブスクライブを解除するイベントの ID です。 |