Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript client


May 09, 2021 CoffeeScript


Table of contents


Client

Problem

You want to use the services provided on the network.

Solution

Create a basic TCP client.

In node .js

net = require 'net'

domain = 'localhost'
port = 9001

connection = net.createConnection port, domain

connection.on 'connect', () ->
    console.log "Opened connection to #{domain}:#{port}."

connection.on 'data', (data) ->
    console.log "Received: #{data}"
    connection.end()

Use the example

Basic Server is accessible:

$ coffee basic-client.coffee
Opened connection to localhost:9001
Received: Hello, World!

Discuss

The most important work occurs during connection.on 'data' processing, when the client receives a response from the server and is most likely to schedule an answer to it.

See also Basic Server, Bi-Directional Client, and Bi-Directional Server.

Practice

  • Add support for selected target domains and ports based on command line parameters or profiles.