-
-
Notifications
You must be signed in to change notification settings - Fork 0
Module: Socket
Aviril edited this page Sep 6, 2022
·
13 revisions
Socket APIs establishes bidirectional communication b/w client & the server unlike REST APIs. Due to it being highly duplex, its suitable & reliable especially for real-time communication. Since other 3rd party networking solution being a bloatware & not as per our taste, we had to build our own lightweight socket solution. Moreover, ws
by default doesn't comes in handy state out of the box & is also deprived of support to execute on local network beside remote which vNetwork's Socket API implementation tries to fulfill.
const socket: cSocket = cServer.socket.create(
string: route,
object: options // (Optional)
)
options: {
// Heartbeat options are shared
heartbeat: {
interval: 10000, // (Optional): Interval at which heartbeat should be executed
timeout: 60000 // (Optional): Duration b/w each heartbeat
},
// Reconnection options are only available on client side
reconnection: {
attempts: -1, // (Optional): Number of attempts before onClientDisconnect is reached. [Note: -1 = infinite attempts]
interval: 2500 // (Optional): Duration b/w each attempt
}
}
const bool: state = cServer.socket.destroy(
string: route
)
const bool: isRouteVoid = cServer.socket.isVoid(
string: route
)
const socket: cSocket = cServer.socket.fetch(
string: route
)
const object: result = cServer.socket.fetchSockets()
// Client Syntax:
cSocket.onPreHeartbeat = function(
int: id,
int: deltaTick
) {}
// Server Syntax:
cSocket.onPreHeartbeat = function(
string: client,
int: id,
int: deltaTick
) {}
// Client Syntax:
cSocket.onHeartbeat = function(
int: id,
int: deltaTick
) {}
// Server Syntax:
cSocket.onHeartbeat = function(
string: client,
int: id,
int: deltaTick
) {}
cSocket.onConnectionError = function(
object: error
) {}
cSocket.onClientConnect = function(
string: client
) {}
cSocket.onClientReconnect = function(
string: client,
int: currentAttempt,
int: maxAttempts
) {}
cSocket.onClientDisconnect = function(
string: client,
string: reason
) {}
reason: [
"version-mismatch", // Used when client/server's version doesn't match
"heartbeat-timeout", // Used when client/server's heartbeat gets timed-out
"server-nonexistent", // Used when client tries to connect to a non-existing/invalid socket URL
"server-disconnected", // Used when server shuts down
"client-disconnected", // Used when client disconnects from server
"client-reconnection-expired" // Used when client's reconnect attempts gets expired
]
cSocket.onServerConnect = function() {}
cSocket.onServerDisconnect = function(
object <date>: timestamp_start, // Timestamp since when server started
object <date>: timestamp_end, // Timestamp when the server shutdown
int: deltaTick // Run duration
) {}
const bool: state = cSocket.isInstance()
const bool: state = cSocket.isConnected(
string: client
)
const bool: state = cSocket.isClient(
string: client
)
const array: result = cSocket.fetchClients()
const bool: state = cSocket.destroy()