-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Olivier Poitrey
committed
Feb 26, 2013
1 parent
5fce8d6
commit 6c5a5ca
Showing
11 changed files
with
191 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
events = require 'events' | ||
Payload = require('./payload').Payload | ||
|
||
class EventPublisher extends events.EventEmitter | ||
constructor: (@pushServices) -> | ||
|
||
publish: (event, data, cb) -> | ||
try | ||
payload = new Payload(data) | ||
payload.event = event | ||
catch e | ||
# Invalid payload (empty, missing key or invalid key format) | ||
cb(-1) if cb | ||
return | ||
|
||
@.emit(event.name, event, payload) | ||
|
||
event.exists (exists) => | ||
if not exists | ||
cb(0) if cb | ||
return | ||
|
||
try | ||
# Do not compile templates before to know there's some subscribers for the event | ||
# and do not start serving subscribers if payload won't compile | ||
payload.compile() | ||
catch e | ||
# Invalid payload (templates doesn't compile) | ||
cb(-1) if cb | ||
return | ||
|
||
event.forEachSubscribers (subscriber, subOptions, done) => | ||
# action | ||
@pushServices.push(subscriber, subOptions, payload, done) | ||
, (totalSubscribers) => | ||
# finished | ||
if totalSubscribers > 0 | ||
# update some event' stats | ||
event.log => | ||
cb(totalSubscribers) if cb | ||
else | ||
# if there is no subscriber, cleanup the event | ||
event.delete => | ||
cb(0) if cb | ||
|
||
exports.EventPublisher = EventPublisher |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
policyFile = '<?xml version="1.0"?>' + | ||
'<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">' + | ||
'<cross-domain-policy>' + | ||
'<site-control permitted-cross-domain-policies="master-only"/>' + | ||
'<allow-access-from domain="*" secure="false"/>' + | ||
'<allow-http-request-headers-from domain="*" headers="Accept"/>' + | ||
'</cross-domain-policy>' | ||
|
||
exports.setup = (app, authorize, eventPublisher) -> | ||
# In order to support access from flash apps | ||
app.get '/crossdomain.xml', (req, res) -> | ||
res.set 'Content-Type', 'application/xml' | ||
res.send(policyFile) | ||
|
||
app.options '/subscribe', authorize('listen'), (req, res) -> | ||
res.set | ||
'Content-Type': 'text/event-stream', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'GET' | ||
'Access-Control-Max-Age': '86400' | ||
res.end() | ||
|
||
app.post '/subscribe', authorize('listen'), (req, res) -> | ||
unless req.accepts('text/event-stream') | ||
res.send 406 | ||
return | ||
|
||
unless typeof req.body.events is 'string' | ||
res.send 400 | ||
return | ||
|
||
eventNames = req.body.events.split ',' | ||
|
||
req.socket.setTimeout(Infinity); | ||
req.socket.setNoDelay(true); | ||
res.set | ||
'Content-Type': 'text/event-stream', | ||
'Cache-Control': 'no-cache', | ||
'Access-Control-Allow-Origin': '*', | ||
'Connection': 'close' | ||
|
||
if req.get('User-Agent')?.indexOf('MSIE') != -1 | ||
# Work around MSIE bug preventing Progress handler from behing thrown before first 2048 bytes | ||
# See http://forums.adobe.com/message/478731 | ||
res.write new Array(2048).join('\n') | ||
|
||
sendEvent = (event, payload) -> | ||
data = | ||
event: event.name | ||
title: payload.title | ||
message: payload.msg | ||
data: payload.data | ||
|
||
res.write("data: " + JSON.stringify(data) + "\n\n") | ||
|
||
antiIdleInterval = setInterval -> | ||
res.write "\n" | ||
, 10000 | ||
|
||
res.socket.on 'close', => | ||
clearInterval antiIdleInterval | ||
for eventName in eventNames | ||
eventPublisher.removeListener eventName, sendEvent | ||
|
||
for eventName in eventNames | ||
eventPublisher.addListener eventName, sendEvent |
This file contains 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 file contains 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 file contains 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
Oops, something went wrong.