Skip to content

Instantly share code, notes, and snippets.

@Ficik
Created March 17, 2013 18:29
Show Gist options
  • Save Ficik/5182942 to your computer and use it in GitHub Desktop.
Save Ficik/5182942 to your computer and use it in GitHub Desktop.
w20-2-statefull
net = require 'net'
server = net.createServer (con) ->
con.setEncoding 'utf8'
items = no
process = (action, value) ->
return "Commands: open, add, process" unless action in ["open", "add", "process"]
if action == "open"
return "order already exists" if items
items = []
return "opened"
if action == "add"
return "open order first" unless items
return "nothing to add" unless value
items.push value
return "added"
if action == "process"
return "nothing to process" unless items
[order, items] = [items, no]
return "processed: #{order.join ", "}"
con.on 'data', (data) ->
[_, action, value] = /([^ ]+) ?(.*)/.exec(data).map?((x) -> x.trim()).filter((x) -> x != '')
con.write process(action, value) + "\n"
server.listen 8080, () ->
console.log "statefull server started"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment