This object implements client sockets (also called just "sockets").
A socket is an endpoint for communication between two machines.
1 Socket Client
SocketClient Object can be created invoking a new Ax.net.SocketClient(hostname, port) constructor.
Copy
<script> var socketClient = new Ax.net.SocketClient("192.168.10.11", 4728); </script>
Return | Method | Description |
---|---|---|
SocketClient | setTcpNoDelay(boolean mode) | Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). |
SocketClient | setTimeout(int timeout) | Enable/disable a connection timeout with the specified timeout, in milliseconds. |
SocketClient | write(String message) | Send message to server thru socket connection. |
SocketClient | read() | Read data from server thru socket connection. |
1.1 Examples
1.1.1 Example 1
The following example, shows a basic connection to a socked endpoint, sends a message and gets a response.
Copy
<script> var socket = new Ax.net.SocketClient("news.google.com", 80) .addConnectEvent((socket) => { console.log("Client socket connected " + socket); }) .addDisconnectEvent((socket) => { console.log("Client disconnected:" + socket); }) ; socket.connect(); var ex = socket.write(`010120081101161501CODIGO USUARIOPASSWORD 101077776000 0000046007205 00 00000000000 020020080509 . 102000000071278600001 10500002000002000000 0199 `); var s = socket.read(); console.log(s); </script>
Client socket connected Socket[addr=news.google.com/142.250.184.14,port=80,localport=60384]
[B@7c4ae7ce
2 Socket Server
SocketServer Object can be created invoking a new Ax.net.SocketServer(port) constructor.
Copy
<script> var socketServer = new Ax.net.SocketServer(4728); </script>
2.1 Examples
2.1.1 Example 1
The following example, shows a basic connection to a socked endpoint, sends a message and gets a response.
Copy
<script> var server = new Ax.net.SocketServer(4728) .setMaxConnections(20) .addConnectEvent((socket) => { console.log("Server socket connected " + socket); }) .addDisconnectEvent((socket) => { console.log("Server disconnected:" + socket); }) .addPacketReceivedEvent((socketHandler, packet) => { console.log("Server received: [" + new Ax.lang.String(packet) + "]"); socketHandler.sendPacket(packet); }) ; server.start(); var socket = new Ax.net.SocketClient("localhost", 4728) .addConnectEvent((socket) => { console.log("Client socket connected " + socket); }) .addDisconnectEvent((socket) => { console.log("Client disconnected:" + socket); }) .addPacketReceivedEvent((socketClient, packet) => { console.log("Client received:" + packet); }) ; socket.connect(); socket.write("Hello world"); socket.write("Lorem ipsilum"); socket.await(); Ax.lang.System.sleep(1500); </script>
Server socket connected Socket[addr=/127.0.0.1,port=52636,localport=4728]
Client socket connected Socket[addr=localhost/127.0.0.1,port=4728,localport=52636]
Server received: [Hello world]
Server received: [Lorem ipsilum]