mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
fmt
This commit is contained in:
@ -14,11 +14,7 @@ impl Actor for AutobahnWebSocket {
|
||||
}
|
||||
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for AutobahnWebSocket {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
ctx: &mut Self::Context,
|
||||
) {
|
||||
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
||||
if let Ok(msg) = msg {
|
||||
match msg {
|
||||
ws::Message::Text(text) => ctx.text(text),
|
||||
|
@ -1,7 +1,5 @@
|
||||
use actix_files::{Files, NamedFile};
|
||||
use actix_web::{
|
||||
middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder,
|
||||
};
|
||||
use actix_web::{middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder};
|
||||
use actix_web_actors::ws;
|
||||
|
||||
mod message;
|
||||
@ -14,10 +12,7 @@ async fn index() -> impl Responder {
|
||||
NamedFile::open_async("./static/index.html").await.unwrap()
|
||||
}
|
||||
|
||||
async fn chat_ws(
|
||||
req: HttpRequest,
|
||||
stream: web::Payload,
|
||||
) -> Result<impl Responder, Error> {
|
||||
async fn chat_ws(req: HttpRequest, stream: web::Payload) -> Result<impl Responder, Error> {
|
||||
ws::start(WsChatSession::default(), &req, stream)
|
||||
}
|
||||
|
||||
|
@ -20,12 +20,7 @@ impl WsChatServer {
|
||||
Some(room)
|
||||
}
|
||||
|
||||
fn add_client_to_room(
|
||||
&mut self,
|
||||
room_name: &str,
|
||||
id: Option<usize>,
|
||||
client: Client,
|
||||
) -> usize {
|
||||
fn add_client_to_room(&mut self, room_name: &str, id: Option<usize>, client: Client) -> usize {
|
||||
let mut id = id.unwrap_or_else(rand::random::<usize>);
|
||||
|
||||
if let Some(room) = self.rooms.get_mut(room_name) {
|
||||
@ -50,12 +45,7 @@ impl WsChatServer {
|
||||
id
|
||||
}
|
||||
|
||||
fn send_chat_message(
|
||||
&mut self,
|
||||
room_name: &str,
|
||||
msg: &str,
|
||||
_src: usize,
|
||||
) -> Option<()> {
|
||||
fn send_chat_message(&mut self, room_name: &str, msg: &str, _src: usize) -> Option<()> {
|
||||
let mut room = self.take_room(room_name)?;
|
||||
|
||||
for (id, client) in room.drain() {
|
||||
|
@ -101,11 +101,7 @@ impl Handler<ChatMessage> for WsChatSession {
|
||||
}
|
||||
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
ctx: &mut Self::Context,
|
||||
) {
|
||||
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
||||
let msg = match msg {
|
||||
Err(_) => {
|
||||
ctx.stop();
|
||||
|
@ -68,11 +68,7 @@ impl Decoder for ChatCodec {
|
||||
impl Encoder<ChatResponse> for ChatCodec {
|
||||
type Error = io::Error;
|
||||
|
||||
fn encode(
|
||||
&mut self,
|
||||
msg: ChatResponse,
|
||||
dst: &mut BytesMut,
|
||||
) -> Result<(), Self::Error> {
|
||||
fn encode(&mut self, msg: ChatResponse, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
||||
let msg = json::to_string(&msg).unwrap();
|
||||
let msg_ref: &[u8] = msg.as_ref();
|
||||
|
||||
@ -112,11 +108,7 @@ impl Decoder for ClientChatCodec {
|
||||
impl Encoder<ChatRequest> for ClientChatCodec {
|
||||
type Error = io::Error;
|
||||
|
||||
fn encode(
|
||||
&mut self,
|
||||
msg: ChatRequest,
|
||||
dst: &mut BytesMut,
|
||||
) -> Result<(), Self::Error> {
|
||||
fn encode(&mut self, msg: ChatRequest, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
||||
let msg = json::to_string(&msg).unwrap();
|
||||
let msg_ref: &[u8] = msg.as_ref();
|
||||
|
||||
|
@ -2,9 +2,7 @@ use std::time::{Duration, Instant};
|
||||
|
||||
use actix::prelude::*;
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::{
|
||||
middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder,
|
||||
};
|
||||
use actix_web::{middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder};
|
||||
use actix_web_actors::ws;
|
||||
|
||||
mod codec;
|
||||
@ -103,11 +101,7 @@ impl Handler<session::Message> for WsChatSession {
|
||||
|
||||
/// WebSocket message handler
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
ctx: &mut Self::Context,
|
||||
) {
|
||||
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
||||
let msg = match msg {
|
||||
Err(_) => {
|
||||
ctx.stop();
|
||||
|
@ -9,8 +9,7 @@ use std::{
|
||||
use actix::*;
|
||||
use actix_files::{Files, NamedFile};
|
||||
use actix_web::{
|
||||
middleware::Logger, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||
Responder,
|
||||
middleware::Logger, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||
};
|
||||
use actix_web_actors::ws;
|
||||
|
||||
|
@ -105,11 +105,7 @@ impl Handler<server::Message> for WsChatSession {
|
||||
|
||||
/// WebSocket message handler
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
ctx: &mut Self::Context,
|
||||
) {
|
||||
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
||||
let msg = match msg {
|
||||
Err(_) => {
|
||||
ctx.stop();
|
||||
|
@ -3,9 +3,7 @@
|
||||
//! Open `http://localhost:8080/` in browser to test.
|
||||
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::{
|
||||
middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||
};
|
||||
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web_actors::ws;
|
||||
|
||||
mod server;
|
||||
|
@ -55,11 +55,7 @@ impl Actor for MyWebSocket {
|
||||
|
||||
/// Handler for `ws::Message`
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWebSocket {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
ctx: &mut Self::Context,
|
||||
) {
|
||||
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
||||
// process websocket messages
|
||||
println!("WS: {:?}", msg);
|
||||
match msg {
|
||||
|
Reference in New Issue
Block a user