PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/lib/socket.io/transports/htmlfile.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 84 lines | 42 code | 15 blank | 27 comment | 2 complexity | 152d8689ecddb0a9fb90e8bbdf0dd4b3 MD5 | raw file
 1
 2/*!
 3 * Socket.IO - transports - HTMLFile
 4 * Copyright (c) 2010-2011 Guillermo Rauch <guillermo@learnboost.com>
 5 * MIT Licensed
 6 */
 7
 8var Client = require('../client')
 9  , qs = require('querystring');
10
11/**
12 * Expose `HTMLFile`.
13 */
14
15module.exports = HTMLFile;
16
17/**
18 * Initialize a `HTMLFile` client.
19 *
20 * @api private
21 */
22
23function HTMLFile() {
24  Client.apply(this, arguments);
25};
26
27/**
28 * Inherit from `Client.prototype`.
29 */
30
31HTMLFile.prototype.__proto__ = Client.prototype;
32
33/**
34 * Connection implementation.
35 * 
36 * @api private
37 */
38
39HTMLFile.prototype._onConnect = function(req, res){
40  var self = this
41    , body = '';
42
43  switch (req.method){
44    case 'GET':
45      Client.prototype._onConnect.call(this, req, res);
46      res.useChunkedEncodingByDefault = true;
47      res.shouldKeepAlive = true;
48      res.setHeader('Content-Type', 'text/html');
49      res.setHeader('Connection', 'keep-alive');
50      res.setHeader('Transfer-Encoding', 'chunked');
51      res.write('<html><body>' + new Array(245).join(' '));
52      this._payload();
53      break;
54      
55    case 'POST':
56      req.on('data', function(message){ body += message; });
57      req.on('end', function(){
58        try {
59          var msg = qs.parse(body);
60          self._onMessage(msg.data);
61        } catch(e){
62          self.listener.log('htmlfile message handler error - ' + e.stack);
63        }
64
65        res.setHeader('Content-Type', 'text/plain');
66        res.end('ok');
67      });
68      break;
69  }
70};
71
72/**
73 * Write implementation.
74 * 
75 * @param {String} message
76 * @api private
77 */
78  
79HTMLFile.prototype._write = function(message){
80  if (this._open) {
81    message = JSON.stringify(message);
82    this.response.write('<script>parent.s._(' + message + ', document);</script>');
83  }
84};