Created
March 17, 2013 18:29
-
-
Save Ficik/5182942 to your computer and use it in GitHub Desktop.
w20-2-statefull
This file contains 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
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