Struct rust_socketio::SocketBuilder [−][src]
pub struct SocketBuilder { /* fields omitted */ }
Expand description
A builder class for a socket.io
socket. This handles setting up the client and
configuring the callback, the namespace and metadata of the socket. If no
namespace is specified, the default namespace /
is taken. The connect
method
acts the build
method and returns a connected Socket
.
Implementations
impl SocketBuilder
[src]
impl SocketBuilder
[src]pub fn new<T: Into<String>>(address: T) -> Self
[src]
pub fn new<T: Into<String>>(address: T) -> Self
[src]Create as client builder from a URL. URLs must be in the form
[ws or wss or http or https]://[domain]:[port]/[path]
. The
path of the URL is optional and if no port is given, port 80
will be used.
Example
use rust_socketio::{SocketBuilder, Payload}; use serde_json::json; let callback = |payload: Payload, _| { match payload { Payload::String(str) => println!("Received: {}", str), Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data), } }; let mut socket = SocketBuilder::new("http://localhost:4200") .set_namespace("/admin") .expect("illegal namespace") .on("test", callback) .connect() .expect("error while connecting"); // use the socket let json_payload = json!({"token": 123}); let result = socket.emit("foo", json_payload); assert!(result.is_ok());
pub fn set_namespace<T: Into<String>>(self, namespace: T) -> Result<Self, Error>
[src]
pub fn set_namespace<T: Into<String>>(self, namespace: T) -> Result<Self, Error>
[src]Sets the target namespace of the client. The namespace must start
with a leading /
. Valid examples are e.g. /admin
, /foo
.
pub fn on<F>(self, event: &str, callback: F) -> Self where
F: FnMut(Payload, Socket) + 'static + Sync + Send,
[src]
pub fn on<F>(self, event: &str, callback: F) -> Self where
F: FnMut(Payload, Socket) + 'static + Sync + Send,
[src]Registers a new callback for a certain socketio::event::Event
. The event could either be
one of the common events like message
, error
, connect
, close
or a custom
event defined by a string, e.g. onPayment
or foo
.
Example
use rust_socketio::{SocketBuilder, Payload}; let callback = |payload: Payload, _| { match payload { Payload::String(str) => println!("Received: {}", str), Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data), } }; let socket = SocketBuilder::new("http://localhost:4200") .set_namespace("/admin") .expect("illegal namespace") .on("test", callback) .on("error", |err, _| eprintln!("Error: {:#?}", err)) .connect();
pub fn set_tls_config(self, tls_config: TlsConnector) -> Self
[src]
pub fn set_tls_config(self, tls_config: TlsConnector) -> Self
[src]Uses a preconfigured TLS connector for secure cummunication. This configures
both the polling
as well as the websocket
transport type.
Example
use rust_socketio::{SocketBuilder, Payload}; use native_tls::TlsConnector; let tls_connector = TlsConnector::builder() .use_sni(true) .build() .expect("Found illegal configuration"); let socket = SocketBuilder::new("http://localhost:4200") .set_namespace("/admin") .expect("illegal namespace") .on("error", |err, _| eprintln!("Error: {:#?}", err)) .set_tls_config(tls_connector) .connect();
pub fn set_opening_header<K: IntoHeaderName>(
self,
key: K,
val: HeaderValue
) -> Self
[src]
pub fn set_opening_header<K: IntoHeaderName>(
self,
key: K,
val: HeaderValue
) -> Self
[src]Sets custom http headers for the opening request. The headers will be passed to the underlying transport type (either websockets or polling) and then get passed with every request thats made. via the transport layer.
Example
use rust_socketio::{SocketBuilder, Payload}; use reqwest::header::{ACCEPT_ENCODING}; let socket = SocketBuilder::new("http://localhost:4200") .set_namespace("/admin") .expect("illegal namespace") .on("error", |err, _| eprintln!("Error: {:#?}", err)) .set_opening_header(ACCEPT_ENCODING, "application/json".parse().unwrap()) .connect();
pub fn connect(self) -> Result<Socket, Error>
[src]
pub fn connect(self) -> Result<Socket, Error>
[src]Connects the socket to a certain endpoint. This returns a connected
Socket
instance. This method returns an std::result::Result::Err
value if something goes wrong during connection.
Example
use rust_socketio::{SocketBuilder, Payload}; use serde_json::json; let mut socket = SocketBuilder::new("http://localhost:4200") .set_namespace("/admin") .expect("illegal namespace") .on("error", |err, _| eprintln!("Socket error!: {:#?}", err)) .connect() .expect("connection failed"); // use the socket let json_payload = json!({"token": 123}); let result = socket.emit("foo", json_payload); assert!(result.is_ok());
Auto Trait Implementations
impl !RefUnwindSafe for SocketBuilder
impl Send for SocketBuilder
impl Sync for SocketBuilder
impl Unpin for SocketBuilder
impl !UnwindSafe for SocketBuilder
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Instrument for T
[src]
impl<T> Instrument for T
[src]fn instrument(self, span: Span) -> Instrumented<Self>
[src]
fn instrument(self, span: Span) -> Instrumented<Self>
[src]Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
fn in_current_span(self) -> Instrumented<Self>
[src]
fn in_current_span(self) -> Instrumented<Self>
[src]impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,