pub trait DiscordIpc {
    // Required method
    fn close(&mut self) -> Result<(), Box<dyn Error + 'static, Global>>;

    // Provided methods
    fn connect(&mut self) -> Result<(), Box<dyn Error + 'static, Global>> { ... }
    fn reconnect(&mut self) -> Result<(), Box<dyn Error + 'static, Global>> { ... }
    fn send_handshake(&mut self) -> Result<(), Box<dyn Error + 'static, Global>> { ... }
    fn send(
        &mut self,
        data: Value,
        opcode: u8
    ) -> Result<(), Box<dyn Error + 'static, Global>> { ... }
    fn recv(&mut self) -> Result<(u32, Value), Box<dyn Error + 'static, Global>> { ... }
    fn set_activity(
        &mut self,
        activity_payload: Activity<'_>
    ) -> Result<(), Box<dyn Error + 'static, Global>> { ... }
    fn clear_activity(&mut self) -> Result<(), Box<dyn Error + 'static, Global>> { ... }
}
Expand description

A client that connects to and communicates with the Discord IPC.

Implemented via the DiscordRPCClient struct.

Required Methods§

fn close(&mut self) -> Result<(), Box<dyn Error + 'static, Global>>

Closes the Discord IPC connection. Implementation is dependent on platform.

Provided Methods§

fn connect(&mut self) -> Result<(), Box<dyn Error + 'static, Global>>

Connects the client to the Discord IPC.

This method attempts to first establish a connection, and then sends a handshake.

Errors

Returns an Err variant if the client fails to connect to the socket, or if it fails to send a handshake.

Examples
let mut client = discord_rich_presence::new_client("<some client id>")?;
client.connect()?;

fn reconnect(&mut self) -> Result<(), Box<dyn Error + 'static, Global>>

Reconnects to the Discord IPC.

This method closes the client’s active connection, then re-connects it and re-sends a handshake.

Errors

Returns an Err variant if the client failed to connect to the socket, or if it failed to send a handshake.

Examples
let mut client = discord_rich_presence::new_client("<some client id>")?;
client.connect()?;

client.close()?;
client.reconnect()?;

fn send_handshake(&mut self) -> Result<(), Box<dyn Error + 'static, Global>>

Handshakes the Discord IPC.

This method sends the handshake signal to the IPC. It is usually not called manually, as it is automatically called by connect and/or reconnect.

Errors

Returns an Err variant if sending the handshake failed.

fn send( &mut self, data: Value, opcode: u8 ) -> Result<(), Box<dyn Error + 'static, Global>>

Sends JSON data to the Discord IPC.

This method takes data (serde_json::Value) and an opcode as its parameters.

Errors

Returns an Err variant if writing to the socket failed

Examples
let payload = serde_json::json!({ "field": "value" });
client.send(payload, 0)?;

fn recv(&mut self) -> Result<(u32, Value), Box<dyn Error + 'static, Global>>

Receives an opcode and JSON data from the Discord IPC.

This method returns any data received from the IPC. It returns a tuple containing the opcode, and the JSON data.

Errors

Returns an Err variant if reading the socket was unsuccessful.

Examples
client.connect_ipc()?;
client.send_handshake()?;

println!("{:?}", client.recv()?);

fn set_activity( &mut self, activity_payload: Activity<'_> ) -> Result<(), Box<dyn Error + 'static, Global>>

Sets a Discord activity.

This method is an abstraction of send, wrapping it such that only an activity payload is required.

Errors

Returns an Err variant if sending the payload failed.

fn clear_activity(&mut self) -> Result<(), Box<dyn Error + 'static, Global>>

Works the same as as set_activity but clears activity instead.

Errors

Returns an Err variant if sending the payload failed.

Implementors§