Skip to content

Commit

Permalink
Fix port being placed in the wrong location
Browse files Browse the repository at this point in the history
  • Loading branch information
JarredAllen committed Jun 4, 2021
1 parent 375ec49 commit 0731e7c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 172 deletions.
179 changes: 17 additions & 162 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ mqttrs = "0.2.0"
rustls = { version = "0.19.0", optional = true }
tokio = { version = "1.2.0", features = ["io-util", "macros", "net", "rt", "rt-multi-thread", "sync", "time"] }
tokio-rustls = { version = "0.22.0", optional = true }
tokio-tungstenite = { version = "0.14", optional = true }
tungstenite = { version = "0.13", optional = true }
tokio-tungstenite = { version = "0.14", optional = true, features = ["rustls-tls"] }

[dev-dependencies]
env_logger = "0.7.1"
Expand All @@ -30,5 +29,5 @@ webpki-roots = "0.18.0"
[features]
default = ["tls", "websocket"]
tls = ["rustls", "tokio-rustls"]
websocket = ["tungstenite", "tokio-tungstenite"]
websocket = ["tokio-tungstenite"]
unsafe-logging = []
2 changes: 2 additions & 0 deletions src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl ClientBuilder {
/// Set TCP port to connect to.
///
/// The default value is 1883.
///
/// If using a websocket, this value is ignored. The port should be specified in the host URL.
pub fn set_port(&mut self, port: u16) -> &mut Self {
self.port = Some(port);
self
Expand Down
8 changes: 4 additions & 4 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use http::request::Request;
use bytes::BytesMut;
#[cfg(feature = "websocket")]
use tungstenite::http::Uri;
use tokio_tungstenite::tungstenite::http::Uri;
use crate::{
client::{
builder::ClientBuilder,
Expand Down Expand Up @@ -565,10 +565,10 @@ async fn connect_stream(opts: &ClientOptions) -> Result<AsyncStream> {
}
#[cfg(feature = "websocket")]
ConnectionMode::Websocket => {
let url = format!("{}:{}", opts.host, opts.port);
println!("Connecting to websocket: \"{}\"", url);
// let url = format!("{}:{}", opts.host, opts.port);
// println!("Websocket: \"{}\"", url);
let websocket = tokio_tungstenite::connect_async(
Request::get(url.parse::<Uri>().unwrap()).header("Sec-WebSocket-Protocol", "mqtt").body(()).unwrap()
Request::get(opts.host.parse::<Uri>().unwrap()).header("Sec-WebSocket-Protocol", "mqtt").body(()).unwrap()
).await
.map_err(crate::util::tungstenite_error_to_std_io_error)?.0;
Ok(AsyncStream::WebSocket(websocket))
Expand Down
3 changes: 1 addition & 2 deletions src/util/async_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use tokio::{
#[cfg(feature = "tls")]
use tokio_rustls::client::TlsStream;
#[cfg(feature = "websocket")]
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use tokio_tungstenite::{tungstenite::{protocol::Message, Error}, MaybeTlsStream, WebSocketStream};
#[cfg(feature = "websocket")]
use tungstenite::{protocol::Message, Error};

/// A wrapper for the data connection, which may or may not be encrypted.
pub(crate) enum AsyncStream {
Expand Down
2 changes: 1 addition & 1 deletion tests/mosquitto.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ keyfile certs/serverkey.pem
# =================================================================
listener 1883 127.0.0.1

# Websockets listener
# Websockets listener on localhost:9001
# =================================================================
listener 9001 127.0.0.1
protocol websockets

0 comments on commit 0731e7c

Please sign in to comment.