 
     stop: ->
         @server.close()
-        @ioserver.close()
 
     add: (file) ->
         file.add = true

                key: fs.readFileSync @config.key
                cert: fs.readFileSync @config.cert
            }, @app
            @ioserver = https.createServer {
                key: fs.readFileSync @config.key
                cert: fs.readFileSync @config.cert
            }
        else
            @server = http.createServer @app
            @ioserver = http.createServer()
        @io = new sio @ioserver, { "log level": 0 }


        @app.use require("body-parser").json({limit: '50mb'})


{BasePipe} = require "../pipe"

reloadCode = """
    +function() {
        io.connect(window.location.origin, { path: "/__reload" })
        .on("reconnect", function() { location.reload(); })
        .on("page", function() { location.reload(); })
        .on("css", function() {
            var links = document.getElementsByTagName("link"),
                date = new Date().valueOf();
            for (var i=0; i<links.length; i++) {
                var tag = links[i];
                if (/stylesheet/i.test(tag.rel) && tag.href) {
                    var href = tag.href.replace(/(&|\\?)\\d+/, ""),
                        query = (~href.indexOf("?")?"&":"?") + date;
                    tag.href = href + query;
                }
            }
        });
    }();
"""


reload = (pattern) ->
    script = """
        <script type="text/javascript" src="/__reload/socket.io.js"></script>
        <script type="text/javascript">#{reloadCode}</script>
    """

    return (req, res, next) ->
