Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple chat server using nodejs and coffeescript
net = require 'net'
server = net.createServer()
broadcast = (client, data) ->
client.write(data) if client.writable
for_others = (everyone, me, func) ->
func(other) for other in everyone when me != other
remove_me = (everyone, me) ->
everyone.splice everyone.indexOf(me), 1
clients = []
server.on 'connection', (client) ->
clients.push client
client.name = "#{client.remoteAddress}:#{client.remotePort}"
client.write "[#{client.name}] Hi!\n"
for_others clients, client, (other) ->
broadcast other, "[#{other.name} has joined]\n"
client.on 'data', (data) ->
for_others clients, client, (other) ->
broadcast other, data
client.on 'end', ->
for_others clients, client, (other) ->
broadcast other, "[#{client.name}] has quit]\n"
remove_me clients, client
client.destroy
client.on 'error', (e) ->
console.log(e)
server.listen 3000
console.log 'Server running at :3000'
net = require 'net'
server = net.createServer()
server.on 'connection', (client) ->
client.write '[node] Hi!\n'
client.write '[node] Bye!\n'
client.end
server.listen 3000
console.log 'Server running at :3000'
net = require 'net'
server = net.createServer()
server.on 'connection', (client) ->
client.write '[node] Hi\n'
client.on 'data', (data) ->
console.log data
server.listen 3000
console.log 'Server running at :3000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment