RealTimeSocket | Android Developers

Quicknav Quicknav
Links

- Google Play Developer Console
- Android Developers Blog
- About Android

Android Sites

- Android.com
- Android Developers
- Android Open Source Project

Language
close

- - About
  - Wear
  - TV
  - Auto
- - Get Started
  - Devices
  - Style
  - Patterns
  - Building Blocks
  - Downloads
  - Videos
- - Training
  - API Guides
  - Reference
  - Tools
  - Google Services
  - Samples
- - Google Play
  - Essentials
  - Get Users
  - Engage & Retain
  - Monetize
  - Tools & Reference
  - Developer Stories

## Results

Loading...
public interface

# RealTimeSocket

com.google.android.gms.games.multiplayer.realtime.RealTimeSocket

## Class Overview

Provides a Socket-like interface for a real-time data connection to a participant in a real-time room. Use `getSocketForParticipant(GoogleApiClient, String, String)` to get an instance.

Use `getParcelFileDescriptor()` to get a file descriptor for read/write, or use `getInputStream()` / `getOutputStream()` to get access to an instance of `InputStream` or `OutputStream` respectively.

Calling close() on any of (i) the returned `ParcelFileDescriptor` or, (ii) the `InputStream` or `OutputStream` instances will close the socket.

The Socket-like interface provided is currently modeled on connection-based byte streams, rather than preserving packet semantics. That is, multiple writes may be coalesced or individual writes divided. Clients should avoid calling `write(byte[])` with more than `MAX_UNRELIABLE_MESSAGE_LEN` bytes at a time.

## Summary

Public Methods
abstract void | close() Closes this real-time socket connection with the participant.
abstract InputStream | getInputStream() Use this to read data from this socket.
abstract OutputStream | getOutputStream() Instance of `OutputStream` to write data to.
abstract ParcelFileDescriptor | getParcelFileDescriptor() Returns the underlying file descriptor for direct read/write access.
abstract boolean | isClosed() Use this to check if the socket is closed.

## Public Methods

#### public abstract void close ()

Closes this real-time socket connection with the participant.

##### Throws

IOException | on error.

#### public abstract InputStream getInputStream ()

Use this to read data from this socket. Calling close() on the InputStream will close the socket.

##### Returns

- An instance of `InputStream`.

##### Throws

IOException | on error.

#### public abstract OutputStream getOutputStream ()

Instance of `OutputStream` to write data to. Calling `close()` on the OutputStream will close the socket.

Note: The maximum (unfragmented) packet size supported through this API is `MAX_UNRELIABLE_MESSAGE_LEN` bytes (excluding internal protocol headers). Make sure that the byte[] data passed to `write(byte[])` honors this limit - otherwise the packet will be dropped and an error message will be sent to the log.

Do not call `flush()` on the `OutputStream` instance that you obtained from this routine. To send each message as a single datagram, implement your own message parsing logic to distinguish message boundaries. This allows partial messages and a non-integral number of messages to be delivered using `RealTimeSocket`.

##### Returns

- An instance of `OutputStream`.

##### Throws

IOException | on error.

#### public abstract ParcelFileDescriptor getParcelFileDescriptor ()

Returns the underlying file descriptor for direct read/write access. Calling close() on the returned ParcelFileDescriptor will close the socket.

##### Returns

- An instance of `ParcelFileDescriptor` or null if the underlying socket is closed.

##### Throws

IOException | in case of error.

#### public abstract boolean isClosed ()

Use this to check if the socket is closed.

##### Returns

- True if the socket is closed.