NewSocket
This function creates a new TCP socket. It can be used either as a server or a client socket. Sockets are useful for example in creating an irc echo script.
Syntax
NewSocket can take 2 different lists of parameters. These are:
Socket NewSocket( string szFuncname )
Socket NewSocket( function pFunc )
Arguments
- szFuncname - This is the name of the handler function for incoming data as a string
- pFunc - This is a pointer to the handler function (closure) for incoming data
Note that you cannot use functions defined inside a class or a namespace. The handler function is called every time the socket receives data from a client or a server it is connected to.
If the socket acts as a client socket, ie. it is connected to a server, the handler function takes two arguments: an instance representing which socket the incoming data originates from(Socket), and a string which represents the incoming data (szData). If the socket is a server socket (ie. different clients can connect to it) the handler function will also take an ID which indicates the client the data is coming from (clientID):
// Client socket: function FunctionName( instance Socket, string szData )
// Server socket: function FunctionName( instance Socket, int clientID, string szData )
(Note: To identify the socket you cannot compare the two instances directly. Make sure you compare the ID properties. For example,)
SomeSocket <- NewSocket( "FunctionName" ); function FunctionName( instance Socket, strin szData ) { if ( Socket.ID == SomeSocket.ID ) print( "Correct Socket Received Data!" ); }
Example 1
function onScriptLoad( ) { p_Socket <- NewSocket( "DataProcess" ); }
This will create a new socket which will process the DataProcess fuction whenever it receives data.
Example 2
function onScriptLoad( ) { p_Socket <- NewSocket( 0, "DataProcess" ); }
This will create a new socket which will process the DataProcess fuction whenever it receives data from ID 0.
Notes
The call onScriptLoad was used in in this example. The function NewSocket was also used. More info about these in the corresponding pages.