Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Ape.Sockets

Pablo Tejada edited this page Sep 22, 2013 · 5 revisions

sockClient » A client socket class

sockServer » A server socket class

Ape.sockClient( port, host, options )

A class constructor. SockClient are used to connect to a socket server.

Parameters

  • port: The port to connect to
  • host: The host to connect to can be either an ip or an hostname
  • options:
    • flushlf: defaults to : false If true onRead is called only when a "\n" is received (data is split around it)

Example

//Instantiating a socket client is simple
var socket = new Ape.sockClient('21', 'example.com', {flushlf: true} );
var socket = new Ape.sockClient('21', 'example.com', {flushlf: true});

socket.onConnect = function() {
    Ape.log("Connected to example.com");
    this.write("Hello\n");
}
// "\n" are removed.
socket.onRead = function(data) {
    Ape.log("Data : " + data);
}

socket.onDisconnect = function() {
    Ape.log("Gone !");
}

sockClient.close()

Used to close the socket connection manually

sockClient.write( data )

Writes the data on the socket

Parameters

  • data: The data to write

sockClient.onConnect()

This function is called when connection has been established

Examples

socket.onConnect = function() {
    Ape.log("We are connected !");
    this.write("Hello\n");
}

sockClient.onDisconnect ()

This function is called when the server we are connected to closes the connection

Examples

socket.onDisconnect = function() {
    Ape.log("Gone !");
}

sockClient.onRead ()

This function is been called when data is received in the socket. One parameter is passed to this function function( data )

Examples

socket.onRead = function( data ) {
    Ape.log(data);
}

Ape.sockServer( port, ip, options)

A class constructor. SockServer is used to create socket server that can accept clients sockets

Parameters

  • port: The port on which to listen
  • ip: The ip on which to bind the server
  • options:
    • flushlf: defaults to : false If true, onRead is called only when a "\n" is received (data is split around it)

Examples

//Here we create a server that listen on all ips on port 80
var socket = new Ape.sockServer("80", "0.0.0.0", {flushlf: true});
var socket = new Ape.sockServer("80", "0.0.0.0", {flushlf: true});

socket.onAccept = function(client) {
    Ape.log("New client !");
    client.write("Hello world\n");
}

socket.onRead = function(client, data) {
    Ape.log('Received data:'+data);
    if(data == 'bye') {
        client.close();
    }
}

sockServer.onAccept()

This function is called when a client connects to the socket. One parameter is passed to this function, the client object function( client )

Examples

socket.onAccept = function(client) {
    Ape.log("New client !");
    client.write("Hello world\n");
}

sockServer.onDisconnect()

This callback is called when an user disconnects.. One parameter is passed to this function, the client which has disconnected function( client )

Examples

socket.onDisconnect = function(client) {
    Ape.log("One client is gone !");
}

sockServer.onRead()

This function is called when a client sends data on his socket. Two parameters are passed to this function, the client who sent data and data sent function( client, data )

Examples

socket.onRead = function(client, data) {
    Ape.log('Received: '+data);
}

Credit goes to the original authors over at ape-project.org