22 console.log(`Server :: Data received from client: ${chunk.toString()}`);
23 });
24
25 socket.on("end", function () {26 console.log("Server :: Closing connection with the client");27 _server.close();28 });29 });
30
31 _server.on("close", () => {
18 _server.on("connection", function (socket) {
19 socket.write("Hello, server.");
20
21 socket.on("data", function (chunk) {22 console.log(`Server :: Data received from client: ${chunk.toString()}`);23 });24
25 socket.on("end", function () {
26 console.log("Server :: Closing connection with the client");
15
16 await testUtils.sleep(100);
17
18 _server.on("connection", function (socket) {19 socket.write("Hello, server.");2021 socket.on("data", function (chunk) {22 console.log(`Server :: Data received from client: ${chunk.toString()}`);23 });2425 socket.on("end", function () {26 console.log("Server :: Closing connection with the client");27 _server.close();28 });29 });30
31 _server.on("close", () => {
32 console.log("Server :: Closing server.");
298 /**
299 * Emitted when the other end of the socket signals the end of transmission.
300 */
301 client.on("end", function () {302 logDebugNetwork("Connection ended.");303 });304
305 });
306}
291 /**
292 * Emitted once the socket is fully closed.
293 */
294 client.on("close", function (hadError: boolean) {295 logDebugNetwork(`Connection closed. Had Errors: ${hadError.toString()}`);296 });297
298 /**
299 * Emitted when the other end of the socket signals the end of transmission.
It is recommended to use arrow functions as callbacks.
Arrow functions can be an attractive alternative to function expressions for callbacks or function arguments.
For example, arrow functions are automatically bound to their surrounding scope/context. This provides an alternative to the pre-ES6 standard of explicitly binding function expressions to achieve similar behavior.
Additionally, arrow functions are:
less verbose, and easier to reason about.
bound lexically regardless of where or when they are invoked.
func(function(a) { return a; }); // ERROR
// prefer: func(a => a)
func(function() { return this.a; }.bind(this)); // ERROR
// prefer: func(() => this.a)
// arrow function callback
func(a => a); // OK
// generator as callback
func(function*() { yield; }); // OK
// function expression not used as callback or function argument
var funcVar = function func(a) { return a; }; // OK
// unbound function expression callback
func(function() { return this.a; }); // OK
// recursive named function callback
func(function recursiveFunc(n) { return n && n + recursiveFunc(n - 1); }); // OK