Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added to obtain the remote IP client #49

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/remote_ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate ws;

struct Server {
ws: ws::Sender,
}

impl ws::Handler for Server {

fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
println!("Remote addres {:?}", self.ws.remote_addr() );
Ok(())
}

fn on_message(&mut self, _: ws::Message) -> ws::Result<()> {
self.ws.close(ws::CloseCode::Normal)
}
}

fn main () {
ws::listen("127.0.0.1:3012", |out| {
Server { ws: out }
}).unwrap()
}
15 changes: 13 additions & 2 deletions src/communication.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::Into;
use std::borrow::Cow;

use std::net::SocketAddr;
use url;
use mio;
use mio::Token;
Expand Down Expand Up @@ -47,6 +47,7 @@ impl Command {
pub struct Sender {
token: Token,
channel: mio::Sender<Command>,
addr: Option<SocketAddr>
}

impl Sender {
Expand All @@ -57,9 +58,20 @@ impl Sender {
Sender {
token: token,
channel: channel,
addr:None
}
}

#[inline]
pub fn set_addr(&mut self, addr:SocketAddr){
self.addr = Some(addr);
}

#[inline]
pub fn remote_addr(&self) -> Option<SocketAddr> {
self.addr
}

/// A Token identifying this sender within the WebSocket.
#[inline]
pub fn token(&self) -> Token {
Expand Down Expand Up @@ -179,4 +191,3 @@ impl Sender {
}

}

14 changes: 9 additions & 5 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ impl<F> Handler<F>
}

#[cfg(feature="ssl")]
pub fn accept(&mut self, eloop: &mut Loop<F>, sock: TcpStream) -> Result<()> {
pub fn accept(&mut self, eloop: &mut Loop<F>, sock: TcpStream, addr: SocketAddr) -> Result<()> {
let factory = &mut self.factory;
let settings = self.settings;

let tok = try!(self.connections.insert_with(|tok| {
let handler = factory.server_connected(Sender::new(tok, eloop.channel()));
let mut sender = Sender::new(tok, eloop.channel());
sender.set_addr(addr);
let handler = factory.server_connected(sender);
Connection::new(tok, sock, handler, settings)
}).ok_or(Error::new(Kind::Capacity, "Unable to add another connection to the event loop.")));

Expand All @@ -243,12 +245,14 @@ impl<F> Handler<F>
}

#[cfg(not(feature="ssl"))]
pub fn accept(&mut self, eloop: &mut Loop<F>, sock: TcpStream) -> Result<()> {
pub fn accept(&mut self, eloop: &mut Loop<F>, sock: TcpStream, addr: SocketAddr) -> Result<()> {
let factory = &mut self.factory;
let settings = self.settings;

let tok = try!(self.connections.insert_with(|tok| {
let handler = factory.server_connected(Sender::new(tok, eloop.channel()));
let mut sender = Sender::new(tok, eloop.channel());
sender.set_addr(addr);
let handler = factory.server_connected(sender);
Connection::new(tok, sock, handler, settings)
}).ok_or(Error::new(Kind::Capacity, "Unable to add another connection to the event loop.")));

Expand Down Expand Up @@ -330,7 +334,7 @@ impl<F> mio::Handler for Handler <F>
}
{
info!("Accepted a new tcp connection from {}.", addr);
if let Err(err) = self.accept(eloop, sock) {
if let Err(err) = self.accept(eloop, sock, addr) {
error!("Unable to build WebSocket connection {:?}", err);
if self.settings.panic_on_new_connection {
panic!("Unable to build WebSocket connection {:?}", err);
Expand Down