mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
websockets text() is more generic
This commit is contained in:
parent
f673dba759
commit
631fe72a46
@ -489,7 +489,7 @@ impl ClientWriter {
|
|||||||
|
|
||||||
/// Send text frame
|
/// Send text frame
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn text<T: Into<String>>(&mut self, text: T) {
|
pub fn text<T: Into<Binary>>(&mut self, text: T) {
|
||||||
self.write(Frame::message(text.into(), OpCode::Text, true, true));
|
self.write(Frame::message(text.into(), OpCode::Text, true, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
/// Send text frame
|
/// Send text frame
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn text<T: Into<String>>(&mut self, text: T) {
|
pub fn text<T: Into<Binary>>(&mut self, text: T) {
|
||||||
self.write(Frame::message(text.into(), OpCode::Text, true, false));
|
self.write(Frame::message(text.into(), OpCode::Text, true, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ use futures::Future;
|
|||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::ws::{Message, WsClientError, WsClient, WsClientWriter};
|
use actix_web::ws;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -71,21 +71,21 @@ fn main() {
|
|||||||
let perf = perf_counters.clone();
|
let perf = perf_counters.clone();
|
||||||
let addr = Arbiter::new(format!("test {}", t));
|
let addr = Arbiter::new(format!("test {}", t));
|
||||||
|
|
||||||
addr.send(actix::msgs::Execute::new(move || -> Result<(), ()> {
|
addr.do_send(actix::msgs::Execute::new(move || -> Result<(), ()> {
|
||||||
let mut reps = report;
|
let mut reps = report;
|
||||||
for _ in 0..concurrency {
|
for _ in 0..concurrency {
|
||||||
let pl2 = pl.clone();
|
let pl2 = pl.clone();
|
||||||
let perf2 = perf.clone();
|
let perf2 = perf.clone();
|
||||||
|
|
||||||
Arbiter::handle().spawn(
|
Arbiter::handle().spawn(
|
||||||
WsClient::new(&ws).connect().unwrap()
|
ws::Client::new(&ws).connect()
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
println!("Error: {}", e);
|
println!("Error: {}", e);
|
||||||
Arbiter::system().send(actix::msgs::SystemExit(0));
|
Arbiter::system().do_send(actix::msgs::SystemExit(0));
|
||||||
()
|
()
|
||||||
})
|
})
|
||||||
.map(move |(reader, writer)| {
|
.map(move |(reader, writer)| {
|
||||||
let addr: SyncAddress<_> = ChatClient::create(move |ctx| {
|
let addr: Addr<Syn, _> = ChatClient::create(move |ctx| {
|
||||||
ChatClient::add_stream(reader, ctx);
|
ChatClient::add_stream(reader, ctx);
|
||||||
ChatClient{conn: writer,
|
ChatClient{conn: writer,
|
||||||
payload: pl2,
|
payload: pl2,
|
||||||
@ -114,7 +114,7 @@ fn parse_u64_default(input: Option<&str>, default: u64) -> u64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct ChatClient{
|
struct ChatClient{
|
||||||
conn: WsClientWriter,
|
conn: ws::ClientWriter,
|
||||||
payload: Arc<String>,
|
payload: Arc<String>,
|
||||||
ts: u64,
|
ts: u64,
|
||||||
bin: bool,
|
bin: bool,
|
||||||
@ -133,9 +133,9 @@ impl Actor for ChatClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stopping(&mut self, _: &mut Context<Self>) -> bool {
|
fn stopping(&mut self, _: &mut Context<Self>) -> Running {
|
||||||
Arbiter::system().send(actix::msgs::SystemExit(0));
|
Arbiter::system().do_send(actix::msgs::SystemExit(0));
|
||||||
true
|
Running::Stop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,15 +171,15 @@ impl ChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Handle server websocket messages
|
/// Handle server websocket messages
|
||||||
impl StreamHandler<Message, WsClientError> for ChatClient {
|
impl StreamHandler<ws::Message, ws::ProtocolError> for ChatClient {
|
||||||
|
|
||||||
fn finished(&mut self, ctx: &mut Context<Self>) {
|
fn finished(&mut self, ctx: &mut Context<Self>) {
|
||||||
ctx.stop()
|
ctx.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle(&mut self, msg: Message, ctx: &mut Context<Self>) {
|
fn handle(&mut self, msg: ws::Message, ctx: &mut Context<Self>) {
|
||||||
match msg {
|
match msg {
|
||||||
Message::Text(txt) => {
|
ws::Message::Text(txt) => {
|
||||||
if txt == self.payload.as_ref().as_str() {
|
if txt == self.payload.as_ref().as_str() {
|
||||||
self.perf_counters.register_request();
|
self.perf_counters.register_request();
|
||||||
self.perf_counters.register_latency(time::precise_time_ns() - self.ts);
|
self.perf_counters.register_latency(time::precise_time_ns() - self.ts);
|
||||||
|
Loading…
Reference in New Issue
Block a user