Skip to content

Instantly share code, notes, and snippets.

@elskwid
Created November 8, 2012 23:26
Show Gist options
  • Save elskwid/4042581 to your computer and use it in GitHub Desktop.
Save elskwid/4042581 to your computer and use it in GitHub Desktop.
quick coffee version of node example in Node Up and Running
net = require "net"
sugar = require "sugar"
chatServer = net.createServer()
clientList = []
chatServer.on "connection", (client) ->
client.name = "#{client.remoteAddress}:#{client.remotePort}"
client.write "Hi! #{client.name}!\n"
console.log "#{client.name} joined"
clientList.push client
console.log clientNames()
client.on "data", (data) ->
console.log data
broadcast data, client
client.on "end", ->
console.log "#{client.name} quit"
clientList.remove (c) -> c.name == client.name
console.log clientNames()
client.on "error", (e) ->
console.log "error: #{e}"
broadcast = (message, client) ->
msg = "#{client.name} says: #{message}"
for c in clientList
c.write msg if c.writable and c isnt client
clientNames = -> clientList.map "name"
chatServer.listen 9000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment