HTTP Transport
OPCUA clients and servers can communicate over HTTP. This is useful for clients and servers that are behind firewalls that only allow HTTP traffic.
OPCUA specification allows to use binary encoding over HTTP. This is called “OPCUA Binary Protocol”. This is the most efficient way to communicate between clients and servers. Despite this SDK also supports to use JSON encoding over HTTP.
HTTP Client
To connect to a server over HTTP, you need to create a client object and call the
connect
method.
Connect method accepts the following parameters:
endpointUrl - The URL of the server.
Different server supports two kinds of schemes for HTTP endpoints.
opc.http://host:port/path
opc.https://host:port/path
Some servers may support also ususal HTTP/HTTPS schemes:
http://host:port/path
https://host:port/path
transportProfile - The transport profile to use. This can be one of the following:
ua.Types.TranportProfileUri.HttpsJson expads to string http://opcfoundation.org/UA-Profile/Transport/https-json
ua.Types.TranportProfileUri.HttpsBinary expands to string http://opcfoundation.org/UA-Profile/Transport/https-uabinary
This parameter is optional. If not provided, the client will try to connect using binary encoding.
Examples
Binary Encoding
The following example shows how to connect to a server using binary encoding over HTTP.
local err = client:connect("opc.http://localhost:"..mako.port.."/opcua/", ua.Types.TranportProfileUri.HttpsBinary)
if err ~= nil then
error("connection failed: "..err)
end
trace("Connected sucessfully")
You can can also omit second parameter, the client will try to connect using binary encoding.
local err = client:connect("opc.https://localhost:"..mako.sslport.."/opcua/")
if err ~= nil then
error("connection failed: "..err)
end
trace("Connected sucessfully")
To connect to a server over HTTPS, you need to pass URL with opc.https:// scheme.
local err = client:connect("opc.https://localhost:"..mako.sslport.."/opcua/", ua.Types.TranportProfileUri.HttpsBinary)
if err ~= nil then
error("connection failed: "..err)
end
trace("Connected sucessfully")
JSON Encoding
JSON encoding is non-standard way to communicate between OPCUA clients and servers. Because of this you can only connect to RealTimeLogic server using JSON encoding.
The following example shows how to connect to a server using JSON encoding over HTTP.
local err = client:connect("opc.https://localhost:"..mako.sslport.."/opcua/", ua.Types.TranportProfileUri.HttpsJson)
if err ~= nil then
error("connection failed: "..err)
end
trace("Connected sucessfully")
HTTP Server
To enable HTTP transport you need to configure server and add corresponding endpoint URL. You don’t need to specify JSON or Binary encoding, the server will automatically detect it by MIME type of the HTTP request.
Also currently server supports only secure policy None for OPCUA messages over HTTP. Security in this case is provided by HTTPS and if you select to use HTTP you should be aware that messages are not encrypted and not signed.
Example
local ua = require("opcua.api")
local config = {
endpoints = {
{
endpointUrl = "opc.http://localhost:9357/opcua",
},
{
endpointUrl = "opc.https://localhost:9357/opcua",
},
{
endpointUrl = "http://localhost:9357/opcua",
},
{
endpointUrl = "https://localhost:9357/opcua",
},
},
}
local server = ua.newServer(config)
server:initialize()
server:run()
local onRequest = server:createHttpDirectory()
--[[
The onRequest function shoulbe called at LSP page when a HTTP request is received.
The function is called with two arguments, the request and the response.
<?lsp
server:onRequest(request, response)
?>
]]
server:shutdown()