Namespace LrSocket
This namespace is used to send and receive data from other processes using sockets.
Example usage:
local LrSocket = import "LrSocket"
local LrTasks = import "LrTasks"
import "LrTasks".startAsyncTask( function()
LrFunctionContext.callWithContext( 'socket_remote', function( context )
local running = true
local sender = LrSocket.bind {
functionContext = context,
port = 0, -- (let the OS assign the port)
mode = "send",
onConnecting = function( socket, port )
-- TODO
end,
onConnected = function( socket, port )
-- TODO
end,
onMessage = function( socket, message )
-- nothing, we don't expect to get any messages back from a send port
end,
onClosed = function( socket )
running = false
end,
onError = function( socket, err )
if err == "timeout" then
socket:reconnect()
end
end,
}
sender:send( "Hello world" )
while running do
LrTasks.sleep( 1/2 ) -- seconds
end
sender:close()
end )
end )
Summary
LrSocket.bind( params )
Opens a socket connection (on localhost) for either reading or writing operations.
Closes a socket connection.
Asks a socket to restablish its connection.
socket:send( message )
Sends a message via a "send" mode socket.
Reports the type of this object.
Functions
- LrSocket.bind( params )
-
Opens a socket connection (on localhost) for either reading or writing operations.
First supported in version 6.0 of the Lightroom SDK.
The socket is automatically closed if the plug-in is disabled or removed by the user.
Parameters
- 1. params
- (table) containing the following named parameters:
- functionContext (
LrFunctionContext
) The function context. - plugin (
_PLUGIN
) Your plug-in object. - port (number) The port number to bind the socket to. If 0, the system automatically chooses a port.
- mode (requred, string) Must be either "send", "receive". In "send" mode, calls to the controller's send method will send data out via the socket. In "receive" mode, data coming in from the socket will be reported via the callback function.
- onConnecting (optional, function( socket, port )) called when the socket begins listing for a connection
- onConnected (optional, function( socket, port)) called when the socket establishes a connection
- onError (optional, function( socket, error)) called when the socket connection encounters an error (ex: "timeout" )
- onMessage (optional, function( socket, message)) called when the socket receives a message
- onClosed (optional, function( socket )) called when th socket connection closes
- functionContext (
Return value
(table) A controller object with the methods:- send( message ): Sends string messages to the socket (mode must be "send").
- reconnect(): tries to re-establish the socket connection (for example, call this after a "timeout" error to retry)
- close(): closes the socket connection. After calling this, send and reconnect can no longer be called on this connection object.
- socket:close()
-
Closes a socket connection.
First supported in version 6.0 of the Lightroom SDK.
- socket:reconnect()
-
Asks a socket to restablish its connection.
First supported in version 6.0 of the Lightroom SDK.
- socket:send( message )
-
Sends a message via a "send" mode socket.
First supported in version 6.0 of the Lightroom SDK.
Parameters
- 1. message
- (string) Message to send.
- socket:type()
-
Reports the type of this object.
First supported in version 6.0 of the Lightroom SDK.
Return value
(string) 'LrSocket'.