Permalink
Browse files

added websockets support via socket.io with demo example

  • Loading branch information...
kadirpekel committed Jun 30, 2011
1 parent 725e785 commit 6101e33974bf3a2dbc7467b3bfee52bc0b924706
Showing with 44 additions and 7 deletions.
  1. +8 −5 README.md
  2. +9 −0 examples/websockets/app.coffee
  3. +20 −0 examples/websockets/main.eco
  4. +4 −0 lib/coffeemate.coffee
  5. +3 −2 package.json
View
@@ -29,11 +29,14 @@ mate.context.highlight = (color, txt) ->
"<span style=\"background-color:#{color}\">#{txt}</span>"
# Route it
-mate
- .get '/greet/:name', ->
- # this is context variable
- @greet_msg = "Hello, #{@req.params.name}"
- @render 'home.eco'
+mate.get '/greet/:name', ->
+ # this is context variable
+ @greet_msg = "Hello, #{@req.params.name}"
+ @render 'home.eco'
+
+# Stream it
+mate.io.sockets.on 'connection', (socket) ->
+ socket.emit 'greet', 'Welcome to coffeemate stream'
# Listen it
mate.listen 3000
@@ -0,0 +1,9 @@
+mate = require '../../lib/coffeemate.coffee'
+
+mate.get '/', ->
+ @render 'main.eco'
+
+mate.io.sockets.on 'connection', (socket, i=0) ->
+ setInterval (-> socket.emit 'news', "Breaking news #{i++}"), 500
+
+mate.listen 3000
@@ -0,0 +1,20 @@
+<html>
+ <head>
+ <script src="/socket.io/socket.io.js"></script>
+ <script>
+
+ var socket = io.connect('http://localhost:3000');
+
+ socket.on('news', function (data) {
+ document.getElementById('newsbox').innerHTML += '<p>' + data + '</p>';
+ });
+
+ </script>
+ </head>
+ <body>
+ <div id="newsbox">
+ <h3>News</h3>
+
+ </div>
+ </body>
+</html>
View
@@ -78,6 +78,10 @@ class Coffeemate extends connect.HTTPServer
@options = renderFunc: eco.render, renderDir: '', renderExt: ''
@routes = []
connect.HTTPServer.call @, []
+
+ # enable socket.io if available
+ try @io = require('socket.io').listen @
+
# Factory method for creating new Coffeemate instances
#
View
@@ -1,5 +1,5 @@
{ "readme": "http://github.com/coffeemate/coffeematel#readme"
-, "version": "0.2.4"
+, "version": "0.3.0"
, "description": "the coffee creamer!"
, "main": "lib/coffeemate.coffee"
, "author": "Kadir Pekel <kadirpekel@gmail.com>"
@@ -8,7 +8,8 @@
, "dependencies":
{
"connect": ">= 1.0.0",
- "eco": ">= 1.0.0"
+ "eco": ">= 1.0.0",
+ "socket.io": ">=0.7.6"
}
, "repository" :
{

0 comments on commit 6101e33

Please sign in to comment.