UNPKG

qws

Version:

An HTML5 Web Sockets Server Module

54 lines (45 loc) 1.5 kB
// Generated by CoffeeScript 1.6.3 var EventEmitter, Message, Server, os, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Message = require('./message').Message; EventEmitter = require('events').EventEmitter; os = require('options-stream'); Server = (function(_super) { __extends(Server, _super); function Server(server, options) { var _this = this; this.server = server; this.options = os({ url: '/ws', deflate: true, min_deflate_length: 32 }, options); if (!this.server.__QWS_NUM) { this.server.__QWS_NUM = 0; } this.server.__QWS_NUM++; server.on('upgrade', function(req, socket) { var e, msg; if (!socket.__QWS_NUM) { socket.__QWS_NUM = _this.server.__QWS_NUM; } try { msg = new Message(req, socket, _this.options); } catch (_error) { e = _error; if ('URLNOTMATCHED' === e.message) { if (0 === --socket.__QWS_NUM) { socket.end('HTTP/1.1 400 Bad Request\r\n\r\nurl not matched\r\n'); } return; } else { throw e; } } _this.emit('connect', msg); }); } return Server; })(EventEmitter); exports.Server = Server;