var sockit = loadSockitPlugin();This function is found in sockit.js, and returns the
window.sockit
object. Then we can start a (TCP) server listening on port 8080 (using IPv4):
var server = sockit.createTcpServer(8080);And start it listening on the given port:
server.listen();Since all of the network calls are asynchronous, the server needs to register a callback function in order to respond to events on the server. The API allows registering
server.addEventListener('ondata', function(event) { var message = event.read(); console.log(message); // log the message event.send("world"); // respond to the client's message });Now whenever the server receives data, the anonymous function provided will be invoked, so that the message received will be logged, and the server will reply with
"world"
. Since the plugin is already loaded, to start a (TCP) client:
var client = sockit.createTcpClient("localhost", 8080);Now, we can listen for responses on the client connection by registering a callback on the client:
client.addEventListener('ondata', function(event) { alert(event.read()); });Finally, we can send data from this client to trigger the data callback on our server:
client.send("hello");
To see this example in action, click here.