Skip to content

Commit

Permalink
Change web client to take a url as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
locka99 committed Apr 14, 2019
1 parent 8078103 commit 8a5d1da
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions samples/simple-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fn main() {
// Optional - enable OPC UA logging
opcua_console_logging::init();

// Use the sample client config to set up a client. The sample config has a number of named
// endpoints one of which is marked as the default.
// Make the client configuration
let mut client = ClientBuilder::new()
.application_name("Simple Client")
.application_uri("urn:SimpleClient")
Expand Down
1 change: 1 addition & 0 deletions samples/web-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ actix-web = "0.7"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
clap = "2.33"

[dependencies.opcua-client]
path = "../../client"
Expand Down
33 changes: 25 additions & 8 deletions samples/web-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[macro_use]
extern crate serde_derive;

use std::path::PathBuf;
use std::sync::{Arc, RwLock};

use clap;

use serde_json;

use actix_web::{
Expand Down Expand Up @@ -64,6 +65,19 @@ fn ws_index(r: &HttpRequest<State>) -> Result<HttpResponse, Error> {
}

fn main() {
// Read command line arguments
let opcua_url = {
let m = clap::App::new("Web Client")
.arg(clap::Arg::with_name("url")
.long("url")
.help("Specify the OPC UA endpoint to connect to")
.takes_value(true)
.default_value("opc.tcp://localhost:4855")
.required(false))
.get_matches();
m.value_of("url").unwrap().to_string()
};

// Optional - enable OPC UA logging
opcua_console_logging::init();

Expand All @@ -73,7 +87,7 @@ fn main() {
// Run opcua server (on this thread). Theoretically, every single web socket could have its own
// client which might make more sense (possibly). This way allows them to share the connection,
// and subscript their interest to it.
opcua_run();
opcua_run(opcua_url);
}

fn http_spawn() {
Expand All @@ -98,12 +112,15 @@ fn http_spawn() {
});
}

fn opcua_run() {
// Use the sample client config to set up a client. The sample config has a number of named
// endpoints one of which is marked as the default.
let config_file = "../client.conf";
let mut client = Client::new(ClientConfig::load(&PathBuf::from(config_file)).unwrap());
if let Ok(session) = client.connect_to_endpoint_id(None) {
fn opcua_run(opcua_url: String) {
// Make a client config
let mut client = ClientBuilder::new()
.application_name("WebSocketClient")
.application_uri("urn:WebSocketClient")
.trust_server_certs(true)
.client().unwrap();

if let Ok(session) = client.connect_to_endpoint((opcua_url.as_ref(), SecurityPolicy::None.to_str(), MessageSecurityMode::None, UserTokenPolicy::anonymous()), IdentityToken::Anonymous) {
let result = subscription_loop(session);
if let Err(result) = result {
println!("ERROR: Got an error while performing action - {}", result);
Expand Down

0 comments on commit 8a5d1da

Please sign in to comment.