express = require "express" io = require "socket.io" net = require "net" app = express.createServer() app.use "/images", express.static "#{process.cwd()}/images" app.set "view engine", "jade" app.set "view options", layout: false app.get "/", (req, res, next) -> res.render "chat" app.listen 8003 forwardMessage = (msg, sender) -> # Forward to web chat users for clientId, clientObject of socket.clients if clientObject isnt sender clientObject.send msg for client in tcpClients if client isnt sender client.write "#{msg}\n" socket = io.listen app socket.on "connection", (client) -> client.send "Hi there" client.on "message", (msg) -> console.log "Got message", msg forwardMessage msg, client client.on "disconnect", -> console.log "Client disconnected" tcpClients = [] tcp = net.createServer (client) -> client.setEncoding "utf-8" client.on "connect", -> tcpClients.push client console.log "Connection from #{socket.remoteAddress}" client.write "Hi, there\n" client.on "data", (msg) -> console.log "Got #{msg}" forwardMessage msg, client tcp.listen 3000, "localhost"