Created
March 17, 2011 14:21
-
-
Save naneau/874398 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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