-
Notifications
You must be signed in to change notification settings - Fork 6
Ape.Sockets
sockClient » A client socket class
- .close » Closes the socket
- .write » Writes on the socket
- .onConnect » Connection has been established callback
- .onDisconnect » Connection closed callback
- .onRead » Data received callback
sockServer » A server socket class
-
.onAccept » Client connected callback
-
.onDisconnect » Client disconnected callback
-
.onRead » Client data received callback
A class constructor. SockClient are used to connect to a socket server.
- 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)
//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 !");
}
Used to close the socket connection manually
Writes the data on the socket
-
data: The data to write
This function is called when connection has been established
socket.onConnect = function() {
Ape.log("We are connected !");
this.write("Hello\n");
}
This function is called when the server we are connected to closes the connection
socket.onDisconnect = function() {
Ape.log("Gone !");
}
This function is been called when data is received in the socket. One parameter is passed to this function function( data )
socket.onRead = function( data ) {
Ape.log(data);
}
A class constructor. SockServer is used to create socket server that can accept clients sockets
- 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)
//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();
}
}
This function is called when a client connects to the socket. One parameter is passed to this function, the client object function( client )
socket.onAccept = function(client) {
Ape.log("New client !");
client.write("Hello world\n");
}
This callback is called when an user disconnects.. One parameter is passed to this function, the client which has disconnected function( client )
socket.onDisconnect = function(client) {
Ape.log("One client is gone !");
}
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 )
socket.onRead = function(client, data) {
Ape.log('Received: '+data);
}
Credit goes to the original authors over at ape-project.org