Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 29 lines (23 sloc) 0.833 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# ***** BEGIN LICENSE BLOCK *****
# Copyright (c) 2011-2012 VMware, Inc.
#
# For the license see COPYING.
# ***** END LICENSE BLOCK *****

utils = require('./utils')
transport = require('./transport')


class EventSourceReceiver extends transport.ResponseReceiver
    protocol: "eventsource"

    doSendFrame: (payload) ->
        # Beware of leading whitespace
        data = ['data: ',
                utils.escape_selected(payload, '\r\n\x00'),
                '\r\n\r\n']
        super(data.join(''))

exports.app =
    eventsource: (req, res) ->
        res.setHeader('Content-Type', 'text/event-stream; charset=UTF-8')
        res.writeHead(200)
        # Opera needs one more new line at the start.
        res.write('\r\n')

        transport.register(req, @, new EventSourceReceiver(req, res, @options))
        return true
Something went wrong with that request. Please try again.