NewSocket

From Liberty Unleashed Wiki
Revision as of 18:10, 3 January 2011 by Windlord (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Home   |   Squirrel Scripting   |   Server Functions   |   Server Events   |   Client Functions   |   Client Events

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:

  1. Socket NewSocket( string szFuncname )
  1. 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 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):

  1. // Client socket:
  2. function FunctionName( instance Socket, string szData )
  1. // Server socket:
  2. 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,)

  1.  
  2. SomeSocket <- NewSocket( "FunctionName" );
  3.  
  4. function FunctionName( instance Socket, strin szData )
  5. {
  6. if ( Socket.ID == SomeSocket.ID ) print( "Correct Socket Received Data!" );
  7. }

Example 1

  1.  
  2. function onScriptLoad( )
  3. {
  4. p_Socket <- NewSocket( "DataProcess" );
  5. }
  6.  

This will create a new socket which will process the DataProcess fuction whenever it receives data.

Example 2

  1.  
  2. function onScriptLoad( )
  3. {
  4. p_Socket <- NewSocket( 0, "DataProcess" );
  5. }
  6.  

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.

Related Functions

Personal tools
Namespaces

Variants
Actions
Navigation
scripting
Toolbox