This repository was archived by the owner on Jun 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Socket
kmussel edited this page Dec 15, 2010
·
6 revisions
CoreJS offers the ability to easily create a TCP socket server with asynchronous callbacks.
Before any clients can connect to a server we need to create a socket to listen and accept connections on a given port.
Create Server:
// Create Server Object this will automatically start listening on the port and accept incoming connections:
var server = new serverSocket(PORT);
// On Client Connection: callback triggered when a client connects to the server:
server.onConnect(function(data){
print("Client has connected. ");
});
// On Client Disconnect
server.onDisconnect( function(data){
print("Client has disconnected. ");
});
// Receiving Data: Callback used when server receives data from the client
server.onData( function(data){
print("Data received from client= " + data);
});Create Client:
// Create Client Object: this will connect to the the server if there is a running server listening for connections
/**
* HOST: string
* PORT: int
**/
var client = new clientSocket(HOST, PORT);
// Receiving Data: Callback triggered when client receives data from the server
client.onData( function(data){
print("Data receieved from server= " + data);
});