Squirrel/Server/Functions/Sockets/NewSocket
(→Arguments) |
(→Arguments) |
||
Line 16: | Line 16: | ||
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. | 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 | + | If the socket acts as a client socket, ie. it is connected to a server, the handler function takes two arguments: an instance representing the socket which 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''): |
<code lang="squirrel">// Client socket: | <code lang="squirrel">// Client socket: |
Latest revision as of 18:10, 3 January 2011
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.
[edit] Syntax
NewSocket can take 2 different lists of parameters. These are:
Socket NewSocket( string szFuncname )
Socket NewSocket( function pFunc )
[edit] 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 the socket which 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!" ); }
[edit] Example 1
function onScriptLoad( ) { p_Socket <- NewSocket( "DataProcess" ); }
This will create a new socket which will process the DataProcess fuction whenever it receives data.
[edit] 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.
[edit] Notes
The call onScriptLoad was used in in this example. The function NewSocket was also used. More info about these in the corresponding pages.