Created
November 8, 2012 23:26
-
-
Save elskwid/4042581 to your computer and use it in GitHub Desktop.
quick coffee version of node example in Node Up and Running
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
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