SocketServerstart()
Creates a listening server that clients can connect to and interact with. CFC's that are created for each client, you will access to the current application scope inside the CFC
Usage
BOOLEAN = SocketServerstart(
port,
cfc,
ip
)
Argument | Summary |
---|---|
port | the port to listen for incoming requests on |
cfc | the cfc that will be used for the callback of new connections |
ip | the IP interface to listen on, defaults to all interfaces on the machine [optional] |
Calling
Supports named-parameter calling allowing you to use the function like:
SocketServerstart( port=?, cfc=?, ip=? );
Supports passing parameters as a structure using ArgumentCollection:
SocketServerstart( ArgumentCollection={ port : ?, cfc : ?, ip : ? } );
Extra
The functions, SocketServerStart(), SocketServerStop() and SocketServerGetClients(), are for writing server applications that operate on custom protocols.
The following example shows how you can create a simple server application that will echo back the input received.
<cfset SocketServerStart( port=2001, cfc="openbdtest.functions.socket.clientcfc" )>
This starts the server running listening on port 2001 and echoes back any line received. The CFC will be created for each new connection, that has the following methods:
<cfcomponent> <cffunction name="onConnect"> <cfargument name="socketdata" required="true" /> <cfset Console( "onConnect" ) > <cfset arguments.socketdata.sendLine("Welcome: " & arguments.socketdata.remoteip )> </cffunction> <cffunction name="onDisconnect"> <cfargument name="socketdata" required="true" /> <cfset Console( "onDisconnect" )> </cffunction> <cffunction name="onReadLine"> <cfargument name="socketdata" required="true" /> <cfargument name="line" required="true" /> <cfset arguments.socketdata.sendLine("Rxd:" & arguments.line)> </cffunction> </cfcomponent>
Within the CFC there are 3 main events that can be overridden: onConnect(), onDisconnect() and onReadLine(). The onConnect() will be called when a client first connects and the details about that client will be available in the 'arguments.socketdata' method (see SocketConnect() for more details).
The CFC that is created for this client is a persisted one. In other words, you can set object level variables that will be retained throughout the life of the connected client.
If the server was started within the confines of a CFML application then the application scope will be available inside the CFC call as you are processing the connected client.
When a line is received by the server, the onReadLine() method is called with the line that was received.
You can persist the SocketData in the class if you wish, they are merely passed into each function as convienance.