Socket

Summary
Creates a new socket listening on the given port.
Closes the specified connection.
Closes all current connections but allows new ones.
Sends data synchronously to the given connection.
Sends the same data to all current connections.
Closes all connections and disables the listening socket so that it will not accept any more incoming connections.
This function is called whenever a new connection is accepted but before any data is read from it.
This function is called when data arrives on the socket.
Called whenever a connection is closed, either because the remote host closed it, you closed it or there was an error that caused it to close.
Creates a new client socket.
Closes the connection.
Sends a string synchronously.
Sends the contents of a data instance.
This function is called when data arrives on the socket.
Called whenever the connection is closed, either because the remote host closed it, you closed it or there was an error that caused it to close.

Functions

Socket

Socket(int port)

Creates a new socket listening on the given port.

portThe listening port.

disconnect

bool disconnect(int connection)

Closes the specified connection.  No more data will be received from or sent to it.

parameters

connectionThe connection to close.

returns

trueif the connection existed and was disconnected.
falseotherwise.

disconnectAll

void disconnectAll()

Closes all current connections but allows new ones.

send

bool send(int connection ,
string data)

Sends data synchronously to the given connection.

parameters

connectionThe connection to send the data to.
dataThe data to send as a string.

returns

trueIf the data was sent successfully.
falseOtherwise.

sendAll

sendAll(string data)

Sends the same data to all current connections.

parameters

dataThe data to send.

close

close()

Closes all connections and disables the listening socket so that it will not accept any more incoming connections.  The Socket instance becomes useless at this point; if you want to listen again, you will need to create a new one.

Events

onConnect

callback bool onConnect(int connection ,
string address)

This function is called whenever a new connection is accepted but before any data is read from it.

parameters

connectionThe identifier of the connection.  This is actually its file descriptor.
addressThe address of the connection.

returns

trueIf you would like to accept the connection.
falseOtherwise.

example

var socket = new Socket( 81 );
socket.onConnect = function( connection , address )
{
return ( address.substr( 0 , 7 ) == "192.168" );
}

onData

callback onData(int connection ,
string data)

This function is called when data arrives on the socket.

parameters

connectionThe connection.
dataThe data as a string.

onDisconnect

callback onDisconnect(int connection)

Called whenever a connection is closed, either because the remote host closed it, you closed it or there was an error that caused it to close.

parameters

connectionThe connection that closed.

notes

Don’t try to send data to the connection, it is too late when this event happens.

ClientSocket

Summary
Creates a new client socket.
Closes the connection.
Sends a string synchronously.
Sends the contents of a data instance.
This function is called when data arrives on the socket.
Called whenever the connection is closed, either because the remote host closed it, you closed it or there was an error that caused it to close.

Functions and Properties

ClientSocket

ClientSocket( )

Creates a new client socket.

connect

bool connect(string host ,
int port)

disconnect

disconnect()

Closes the connection.

isConnected

bool isConnected readonly

send

bool send(string data)

Sends a string synchronously.

parameters

dataThe data to send as a string.

returns

trueIf the data was sent successfully.
falseOtherwise.

sendData

bool sendData(Data data)

Sends the contents of a data instance.

close

close()
Closes the connectionsame as disconnect.

Events

onData

callback onData(Data data)

This function is called when data arrives on the socket.

parameters

dataThe data.

onDisconnect

callback onDisconnect( )

Called whenever the connection is closed, either because the remote host closed it, you closed it or there was an error that caused it to close.

notes

Don’t try to send data to the connection, it is too late when this event happens.

Socket(int port)
Creates a new socket listening on the given port.
bool disconnect(int connection)
Closes the specified connection.
void disconnectAll()
Closes all current connections but allows new ones.
bool send(int connection ,
string data)
Sends data synchronously to the given connection.
sendAll(string data)
Sends the same data to all current connections.
close()
Closes all connections and disables the listening socket so that it will not accept any more incoming connections.
callback bool onConnect(int connection ,
string address)
This function is called whenever a new connection is accepted but before any data is read from it.
callback onData(int connection ,
string data)
This function is called when data arrives on the socket.
callback onDisconnect(int connection)
Called whenever a connection is closed, either because the remote host closed it, you closed it or there was an error that caused it to close.
ClientSocket( )
Creates a new client socket.
bool connect(string host ,
int port)
disconnect()
Closes the connection.
bool isConnected readonly
bool send(string data)
Sends a string synchronously.
bool sendData(Data data)
Sends the contents of a data instance.
close()
callback onData(Data data)
This function is called when data arrives on the socket.
callback onDisconnect( )
Called whenever the connection is closed, either because the remote host closed it, you closed it or there was an error that caused it to close.