Skip to content

Instantly share code, notes, and snippets.

@naneau
Created March 17, 2011 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naneau/874398 to your computer and use it in GitHub Desktop.
Save naneau/874398 to your computer and use it in GitHub Desktop.
http = require 'http'
util = require 'util'
host = '127.0.0.1'
port = 10000
# Ping
ping = () ->
console.log "making request"
# Build the request
request = http.request
host: host
port: port
path: '/ping'
method: 'POST'
# Listen to errors
request.on 'error', (error) ->
console.log "Error making ping request to #{host}:#{port}: #{error}"
# We expect a response from a ping
request.on 'response', (response) ->
response.setEncoding 'utf8'
# Fetch all data chunks
data = ''
response.on 'data', (chunk) ->
data += chunk
# Handle the pong on response end
response.on 'end', () ->
console.log "Pong: #{data}"
# Send the ping request
request.write 'PING'
do request.end
setInterval ping, 1000
setInterval ping, 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment