2016-11-11 308 views
3

我正在使用Java OPC-UA客戶端Eclipse Milo。每當我使用服務器的端點URL創建會話時,方法UaTcpStackClient.getEndpoints()將URL更改爲localhostJava OPC-UA客戶端Eclipse Milo端點URL更改爲localhost

String endpointUrl = "opc.tcp://10.8.0.104:48809"; 

EndpointDescription[] endpoints = UaTcpStackClient.getEndpoints(endpointUrl).get(); 

EndpointDescription endpoint = Arrays.stream(endpoints) 
      .filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getSecurityPolicyUri())) 
      .findFirst().orElseThrow(() -> new Exception("no desired endpoints returned")); 

然而endpoint.getEndpointUrl()返回opc.tcp://127.0.0.1:4880/這導致失敗的連接值。

我不知道爲什麼我的OPC URL被更改?

回答

2

這是實施UA客戶端時很常見的問題。

服務器最終負責回收端點的內容,顯然,您連接的端口(錯誤)配置爲在端點URL中返回127.0.0.1。

您需要檢查從服務器獲取的端點,然後根據應用程序的性質,立即將其替換爲新的複製EndpointDescription,其中包含已修改的URL或讓用戶知道並詢問它們首先獲得許可。

無論哪種方式,您需要創建一組新的EndpointDescription s,其中您已在更正URL之前創建了OpcUaClient

或者,您可以弄清楚如何正確配置您的服務器,以便它返回包含公共可訪問主機名或IP地址的端點。

更新:

的代碼替換端點URL可能是這樣一些變化:

private static EndpointDescription updateEndpointUrl(
    EndpointDescription original, String hostname) throws URISyntaxException { 

    URI uri = new URI(original.getEndpointUrl()).parseServerAuthority(); 

    String endpointUrl = String.format(
     "%s://%s:%s%s", 
     uri.getScheme(), 
     hostname, 
     uri.getPort(), 
     uri.getPath() 
    ); 

    return new EndpointDescription(
     endpointUrl, 
     original.getServer(), 
     original.getServerCertificate(), 
     original.getSecurityMode(), 
     original.getSecurityPolicyUri(), 
     original.getUserIdentityTokens(), 
     original.getTransportProfileUri(), 
     original.getSecurityLevel() 
    ); 
} 

警告:這部作品在大多數情況下,但有它不起作用值得注意的情況是,當遠程端點URL包含URL主機名(根據RFC)中不允許使用的字符,例如下劃線(a'_'),不幸的是,該字符允許在例如Windows機器的主機名。因此,您可能需要使用其他一些解析端點URL的方法,而不是依靠URI類來執行此操作。

+0

感謝您的回覆凱文,我明白你想說什麼..我怎麼可以創建一套新的EndpointDescriptions,我沒有看到任何方式或設置方法 –

+0

我已經添加了一些示例代碼。 –

+0

感謝@kevin Herron –