Created
June 24, 2011 13:17
-
-
Save dakatsuka/1044742 to your computer and use it in GitHub Desktop.
https://gist.github.com/1034095 を CoffeeScript で書いてみた
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
| # This program is free software. It comes without any warranty, to | |
| # the extent permitted by applicable law. You can redistribute it | |
| # and/or modify it under the terms of the Do What The Fuck You Want | |
| # To Public License, Version 2, as published by Sam Hocevar. See | |
| # http://sam.zoy.org/wtfpl/COPYING for more details. | |
| sys = require 'sys' | |
| opts = require 'opts' | |
| ws = require 'websocket-server' | |
| redis = require 'redis' | |
| server = ws.createServer() | |
| subscriber = redis.createClient 6379, 'localhost' | |
| publisher = redis.createClient 6379, 'localhost' | |
| opts.parse([ | |
| { | |
| 'short': 'p', | |
| 'long': 'port', | |
| 'description': 'WebSocket Port', | |
| 'value': true, | |
| 'required': true | |
| } | |
| ]) | |
| subscriber.on "error", (err) -> | |
| sys.debug err | |
| publisher.on "error", (err) -> | |
| sys.debug err | |
| subscriber.subscribe "chat" | |
| subscriber.on "message", (channel, message) -> | |
| sys.puts message | |
| server.broadcast message | |
| server.addListener "connection", (connection) -> | |
| sys.puts "client connected: " + connection.id | |
| connection.addListener "message", (message) -> | |
| publisher.publish "chat", message | |
| server.addListener "close", (connection) -> | |
| sys.puts "client disconnected: " + connection.id | |
| server.listen opts.get('port') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment