UNPKG

latte_web

Version:
42 lines 1.22 kB
var latte_lib = require("latte_lib"); var Transport = require("../transport") , parser = require("../parser"); module.exports = WebSocket; function WebSocket(ctx) { Transport.call(this, ctx); var self = this; this.socket = ctx.websocket; this.socket.on("message", this.onData.bind(this)); this.socket.once("close", this.onClose.bind(this)); this.socket.on("error", this.onError.bind(this)); this.socket.on("headers", function(headers) { self.emit("headers", headers); }); this.writable = true; }; latte_lib.inherits(WebSocket, Transport); (function() { this.name = "websocket"; this.handlesUpgrades = true; this.supportsFraming = true; this.onData = function(data) { Transport.prototype.onData.call(this, data); } this.send = function(packets) { var self = this; for(var i = 0, l = packets.length ; i < l ; i++) { parser.encodePacket(packets[i], this.supportsBinary, function(data) { self.writable = false; self.socket.send(data, function(err) { if(err) return self.onError("write error", err.stack); self.writable = true; self.emit("drain"); }); }); } } this.doClose = function(fn) { this.socket.close(); fn && fn(); } }).call(WebSocket.prototype);