1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 09:59:21 +02:00

add websocket example

This commit is contained in:
Nikolay Kim
2017-10-20 17:16:17 -07:00
parent 1db4200621
commit 56c91adce2
13 changed files with 1120 additions and 85 deletions

View File

@ -2,11 +2,14 @@ use std;
use std::rc::Rc;
use std::collections::VecDeque;
use futures::{Async, Stream, Poll};
use futures::sync::oneshot::Sender;
use bytes::Bytes;
use actix::{Actor, ActorState, ActorContext, AsyncContext};
use actix::{Actor, ActorState, ActorContext, AsyncContext,
Handler, Subscriber, ResponseType};
use actix::fut::ActorFuture;
use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCell, SpawnHandle};
use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCell, SpawnHandle,
Envelope, ToEnvelope, RemoteEnvelope};
use route::{Route, Frame};
use httpresponse::HttpResponse;
@ -118,6 +121,25 @@ impl<A> HttpContext<A> where A: Actor<Context=Self> + Route {
}
}
impl<A> HttpContext<A> where A: Actor<Context=Self> + Route {
#[doc(hidden)]
pub fn subscriber<M: 'static>(&mut self) -> Box<Subscriber<M>>
where A: Handler<M>
{
Box::new(self.address.unsync_address())
}
#[doc(hidden)]
pub fn sync_subscriber<M: 'static + Send>(&mut self) -> Box<Subscriber<M> + Send>
where A: Handler<M>,
A::Item: Send,
A::Error: Send,
{
Box::new(self.address.sync_address())
}
}
#[doc(hidden)]
impl<A> Stream for HttpContext<A> where A: Actor<Context=Self> + Route
{
@ -149,22 +171,25 @@ impl<A> Stream for HttpContext<A> where A: Actor<Context=Self> + Route
}
// check wait futures
if let Some(ref mut act) = self.act {
if let Ok(Async::NotReady) = self.wait.poll(act, ctx) {
return Ok(Async::NotReady)
}
if self.wait.poll(act, ctx) {
return Ok(Async::NotReady)
}
let mut prep_stop = false;
loop {
let mut not_ready = true;
if let Ok(Async::Ready(_)) = self.address.poll(act, ctx) {
if self.address.poll(act, ctx) {
not_ready = false
}
self.items.poll(act, ctx);
// check wait futures
if self.wait.poll(act, ctx) {
return Ok(Async::NotReady)
}
// are we done
if !not_ready {
continue
@ -213,3 +238,17 @@ impl<A> Stream for HttpContext<A> where A: Actor<Context=Self> + Route
}
}
}
type ToEnvelopeSender<A, M> =
Sender<Result<<A as ResponseType<M>>::Item, <A as ResponseType<M>>::Error>>;
impl<A, M> ToEnvelope<A, M> for HttpContext<A>
where M: Send + 'static,
A: Actor<Context=HttpContext<A>> + Route + Handler<M>,
<A as ResponseType<M>>::Item: Send, <A as ResponseType<M>>::Item: Send
{
fn pack(msg: M, tx: Option<ToEnvelopeSender<A, M>>) -> Envelope<A>
{
RemoteEnvelope::new(msg, tx).into()
}
}

View File

@ -77,7 +77,7 @@ impl StaticFiles {
let entry = entry.unwrap();
// show file url as relative to static path
let file_url = format!(
"{}{}", self.prefix,
"{}/{}", self.prefix,
entry.path().strip_prefix(&self.directory).unwrap().to_string_lossy());
// if file is a directory, add '/' to the end of the name

View File

@ -269,10 +269,10 @@ pub struct WsWriter;
impl WsWriter {
/// Send text frame
pub fn text<A>(ctx: &mut HttpContext<A>, text: String)
pub fn text<A>(ctx: &mut HttpContext<A>, text: &str)
where A: Actor<Context=HttpContext<A>> + Route
{
let mut frame = wsframe::Frame::message(Vec::from(text.as_str()), OpCode::Text, true);
let mut frame = wsframe::Frame::message(Vec::from(text), OpCode::Text, true);
let mut buf = Vec::new();
frame.format(&mut buf).unwrap();