短いデータ
LINE Planetは、通信中に限られたサイズのデータ(「短いデータ」)をピアに送信できるAPIを提供します。このページでは、通話タイプ別に短いデータを送受信する方法について説明します。
| 対応する通話タイプ | 対応するSDKバージョン |
|---|---|
| 1対1通話、グループ通話 | 1.2 |
共通事項
通話タイプに関係なく、短いデータの送信には以下の事項を適用します。
サイズの制限
短いデータを送信するメソッドは、データ自体と、データタイプを引数として受け取ります。データタイプおよびデータの最大サイズは次のとおりです。
- データタイプ:100バイト(null終端文字を含む)
- データ:800バイト
短いデータの送信結果を得る
sendShortData()は、ネイティブSDKによって送信リクエストが受け付けられたかどうかを示すFuture<bool>を返します。falseの結果は、送信前にリクエストが拒否されたことを意味します(例:サイズが無効)。ネットワークレベルの配信失敗は別途報告されません。
1対1通話
1対1通話では、次のように短いデータを送受信できます。
短いデータを送信する
通話相手に短いデータを送信するには、PlanetKitCallのsendShortData()を使用します。
Future<bool> sendShortDataExample(PlanetKitCall call, String type, String data) async {
final byteData = Uint8List.fromList(utf8.encode(data));
final success = await call.sendShortData(type: type, data: byteData);
if (success) {
print('sendShortData succeeded: type=$type, data=$data');
} else {
print('sendShortData failed');
}
return success;
}
受信した短いデータを処理する
相手はPlanetKitCallEventHandlerのonShortDataReceivedを使用して短いデータを受信できます。
final eventHandler = PlanetKitCallEventHandler(
// ... other required callbacks ...
onShortDataReceived: (PlanetKitCall call, String type, Uint8List data) {
// This is called when short data is received from the peer.
final receivedData = utf8.decode(data);
print('Received short data: type=$type, data=$receivedData');
// Write your own code here to handle the received data.
},
);
グループ通話
グループ通話では、特定のピアまたはすべてのピアに短いデータを送信できます。
特定のピアに短いデータを送信する
特定のピアに短いデータを送信するには、PlanetKitConferenceのsendShortDataToPeer()を使用します。
Future<bool> sendShortDataToSpecificPeerExample(
PlanetKitConference conference,
PlanetKitUserId peerId,
String type,
String data,
) async {
final byteData = Uint8List.fromList(utf8.encode(data));
final success = await conference.sendShortDataToPeer(
peerId: peerId,
type: type,
data: byteData,
);
if (success) {
print('sendShortDataToPeer succeeded: type=$type, data=$data');
} else {
print('sendShortDataToPeer failed');
}
return success;
}
すべてのピアに短いデータを送信する
すべてのピアに短いデータを送信するには、PlanetKitConferenceのsendShortData()を使用します。
Future<bool> sendShortDataToAllPeersExample(
PlanetKitConference conference,
String type,
String data,
) async {
final byteData = Uint8List.fromList(utf8.encode(data));
final success = await conference.sendShortData(type: type, data: byteData);
if (success) {
print('sendShortData to all peers succeeded: type=$type, data=$data');
} else {
print('sendShortData failed');
}
return success;
}
受信した短いデータを処理する
ピアはPlanetKitConferenceEventHandlerのonShortDataReceivedを通じて短いデータを受信できます。
final eventHandler = PlanetKitConferenceEventHandler(
// ... other required callbacks ...
onShortDataReceived: (
PlanetKitConference conference,
PlanetKitUserId senderId,
String type,
Uint8List data,
) {
// This is called when short data is received from a peer in the conference.
final receivedData = utf8.decode(data);
print('Received short data from ${senderId.userId}: type=$type, data=$receivedData');
// Write your own code here to handle the received data.
},
);
関連API
短いデータの送信に関連するAPIは、以下のとおりです。