Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created January 28, 2014 16:49
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 joyrexus/8671490 to your computer and use it in GitHub Desktop.
Save joyrexus/8671490 to your computer and use it in GitHub Desktop.
Ping-pong actorify demo

Quick demo of actorify, which turns any duplex stream into an actor.

The server responds with a pong whenever a client pings.

###
# Quick demo of actorify.
#
# When a client pings the server responds with a pong.
#
###
net = require 'net'
actorify = require 'actorify'
# server
serve = (sock) ->
actor = actorify(sock)
pong = ->
console.log('PING')
actor.send('pong')
actor.on('ping', pong)
net.createServer(serve)
.listen(3000)
# client
sock = net.connect 3000
actor = actorify sock
ping = ->
actor.send 'ping'
actor.once 'pong', -> console.log 'PONG'
setInterval(ping, 500)
/*
# Quick demo of actorify.
#
# When a client pings the server responds with a pong.
#
*/
(function() {
var actor, actorify, net, pong, serve, sock;
net = require('net');
actorify = require('actorify');
// server
serve = function(sock) {
var actor, pong;
actor = actorify(sock);
pong = function() {
console.log('PING');
actor.send('pong');
};
actor.on('ping', pong);
};
net.createServer(serve).listen(3000);
// client
sock = net.connect(3000);
actor = actorify(sock);
ping = function() {
actor.send('ping');
actor.once('pong', function() {
console.log('PONG');
});
};
setInterval(ping, 500);
}).call(this);
{
"name": "ping-pong",
"version": "0.0.1",
"description": "Ping-pong demo of actorify.",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"actorify": "~0.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment