Skip to content

Instantly share code, notes, and snippets.

@samfrons
Created June 29, 2011 02:41
Show Gist options
  • Save samfrons/1052854 to your computer and use it in GitHub Desktop.
Save samfrons/1052854 to your computer and use it in GitHub Desktop.
chat server in node.js
net = require('net')
Array.prototype.remove = (element) ->
for e, i in this when e is element
return this.splice(i, 1)
class Client
constructor: (stream) ->
@stream = stream
@name = null
clients = []
server = net.createServer((stream) ->
client = new Client(stream)
clients.push client
stream.setTimeout 0
stream.setEncoding "utf8"
stream.addListener('connect', ->
stream.write 'Welcome, enter your username:\n'
)
stream.addListener('data', (data) ->
if client.name is null
client.name = data.match /\S+/
stream.write('===========\n')
for c in clients when c isnt client
c.stream.write(client.name + " has joined.\n")
return
matched = data.match /^\/(.*)/
if matched and matched.length > 1
command = matched[1]
if command == 'users'
for c in clients
stream.write("- " + c.name + "\n")
else if command == 'quit'
stream.end()
return
for c in clients when c isnt client
c.stream.write(client.name + ": " + data)
)
stream.addListener('end', ->
clients.remove client
for c in clients
c.stream.write client.name+" has left.\n"
stream.end()
)
)
server.listen 7000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment