Skip to content

Commit

Permalink
Ping pong WS test working
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Feb 16, 2021
1 parent 9b02c3e commit 24ea9f5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/adapters/web_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use crate::util::{OTHER_THREAD_ERR};
use mio::event::{Source};
use mio::net::{TcpStream, TcpListener};

use tungstenite::protocol::{WebSocket, Role, Message};
use tungstenite::protocol::{WebSocket, Message};
use tungstenite::server::{accept as ws_accept};
use tungstenite::client::{client as ws_client};
use tungstenite::handshake::{HandshakeError};
use tungstenite::error::{Error};

use std::sync::{Mutex};
Expand Down Expand Up @@ -44,10 +46,24 @@ impl ActionHandler for WsActionHandler {
type Listener = ServerResource;

fn connect(&mut self, addr: SocketAddr) -> io::Result<ClientResource> {
// Synchronous tcp handshake
let stream = StdTcpStream::connect(addr)?;

// Make it an asynchronous mio TcpStream
stream.set_nonblocking(true)?;
let stream = TcpStream::from_std(stream);
Ok(ClientResource(Mutex::new(WebSocket::from_raw_socket(stream, Role::Client, None))))

// Synchronous waiting for web socket handshake
let mut handshake_result = ws_client(format!("ws://{}/socket", addr), stream);
loop {
match handshake_result {
Ok((ws_socket, _)) => break Ok(ClientResource(Mutex::new(ws_socket))),
Err(HandshakeError::Interrupted(mid_handshake)) => {
handshake_result = mid_handshake.handshake();
}
Err(HandshakeError::Failure(error)) => panic!("Unexpected ws error: {}", error),
}
}
}

fn listen(&mut self, addr: SocketAddr) -> io::Result<(ServerResource, SocketAddr)> {
Expand Down

0 comments on commit 24ea9f5

Please sign in to comment.