Java Code Examples for org.webrtc.DataChannel#Buffer

The following examples show how to use org.webrtc.DataChannel#Buffer . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: P2PPeerConnectionChannel.java    From owt-client-android with Apache License 2.0 7 votes vote down vote up
void sendData(final String message, ActionCallback<Void> callback) {
    Long msgId = ++messageId;
    final JSONObject messageObj = new JSONObject();
    try {
        messageObj.put("id", msgId);
        messageObj.put("data", message);
    } catch (JSONException e) {
        DCHECK(e);
    }
    if (callback != null) {
        sendMsgCallbacks.put(msgId, callback);
    }

    if (localDataChannel == null || localDataChannel.state() != OPEN) {
        queuedMessage.add(messageObj.toString());
        if (localDataChannel == null) {
            createDataChannel();
        }
        return;
    }

    ByteBuffer byteBuffer =
            ByteBuffer.wrap(messageObj.toString().getBytes(Charset.forName("UTF-8")));
    DataChannel.Buffer buffer = new DataChannel.Buffer(byteBuffer, false);
    localDataChannel.send(buffer);
}
 
Example 2
Source File: RespokeDirectConnection.java    From respoke-sdk-android with MIT License 6 votes vote down vote up
/**
 *  Send a message to the remote client through the direct connection.
 *
 *  @param message             The message to send
 *  @param completionListener  A listener to receive a notification on the success of the asynchronous operation
 */
public void sendMessage(String message, final Respoke.TaskCompletionListener completionListener) {
    if (isActive()) {
        JSONObject jsonMessage = new JSONObject();
        try {
            jsonMessage.put("message", message);
            byte[] rawMessage = jsonMessage.toString().getBytes(Charset.forName("UTF-8"));
            ByteBuffer directData = ByteBuffer.allocateDirect(rawMessage.length);
            directData.put(rawMessage);
            directData.flip();
            DataChannel.Buffer data = new DataChannel.Buffer(directData, false);

            if (dataChannel.send(data)) {
                Respoke.postTaskSuccess(completionListener);
            } else {
                Respoke.postTaskError(completionListener, "Error sending message");
            }
        } catch (JSONException e) {
            Respoke.postTaskError(completionListener, "Unable to encode message to JSON");
        }
    } else {
        Respoke.postTaskError(completionListener, "DataChannel not in an open state");
    }
}
 
Example 3
Source File: PeerConnectionChannel.java    From owt-client-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessage(final DataChannel.Buffer buffer) {
    if (disposed) {
        return;
    }
    callbackExecutor.execute(() -> {
        ByteBuffer data = buffer.data;
        final byte[] bytes = new byte[data.capacity()];
        data.get(bytes);
        String message = new String(bytes);
        observer.onDataChannelMessage(key, message);
    });
}
 
Example 4
Source File: PeerVideoActivity.java    From nubo-test with Apache License 2.0 5 votes vote down vote up
public void sendHelloMessage(DataChannel channel) {
    byte[] rawMessage = "Hello Peer!".getBytes(Charset.forName("UTF-8"));
    ByteBuffer directData = ByteBuffer.allocateDirect(rawMessage.length);
    directData.put(rawMessage);
    directData.flip();
    DataChannel.Buffer data = new DataChannel.Buffer(directData, false);
    channel.send(data);
}
 
Example 5
Source File: NBMPeerConnection.java    From webrtcpeer-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessage(DataChannel.Buffer buffer) {
    Log.i(TAG, "[ObservedDataChannel] NBMPeerConnection onMessage");
    for (NBMWebRTCPeer.Observer observer : observers) {
        observer.onMessage(buffer, NBMPeerConnection.this, channel);
    }
}
 
Example 6
Source File: PeerVideoActivity.java    From nubo-test with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(DataChannel.Buffer buffer, NBMPeerConnection connection, DataChannel channel) {
    Log.i(TAG, "[datachannel] Message received: " + buffer.toString());
    sendHelloMessage(channel);
}
 
Example 7
Source File: Observer.java    From webrtc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(DataChannel.Buffer buffer) {
    System.out.println("DEBUG: message received " + buffer.data);

}
 
Example 8
Source File: MediaResourceManager.java    From webrtcpeer-android with Apache License 2.0 2 votes vote down vote up
@Override
public void onMessage(DataChannel.Buffer buffer, NBMPeerConnection connection, DataChannel channel) {

}
 
Example 9
Source File: NBMWebRTCPeer.java    From webrtcpeer-android with Apache License 2.0 2 votes vote down vote up
/**
 * WebRTC event which is triggered when a message is received from a data channel
 * @param buffer The message buffer
 * @param connection The connection for which the data channel belongs to
 * @param channel The data channel which triggered the event
 */
void onMessage(DataChannel.Buffer buffer, NBMPeerConnection connection, DataChannel channel);