UNPKG

iocat

Version:

WebSocket netcat with Socket.io support

144 lines (122 loc) 4.39 kB
// Generated by CoffeeScript 1.10.0 (function() { var Base, WSServer, ws, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, extend = 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; }, hasProp = {}.hasOwnProperty; Base = require('./Base').Base; ws = require('ws'); WSServer = (function(superClass) { extend(WSServer, superClass); function WSServer(options) { var base; this.options = options != null ? options : {}; this.onClientMessage = bind(this.onClientMessage, this); this.onClientError = bind(this.onClientError, this); this.onClientClose = bind(this.onClientClose, this); this.onClientOpen = bind(this.onClientOpen, this); this.onClientConnect = bind(this.onClientConnect, this); this.onWSServerError = bind(this.onWSServerError, this); this.onWSServerConnection = bind(this.onWSServerConnection, this); this.onWSServerListening = bind(this.onWSServerListening, this); this.end = bind(this.end, this); this.send = bind(this.send, this); this._enqueue = bind(this._enqueue, this); this.isActive = bind(this.isActive, this); this.start = bind(this.start, this); this.log('constructor'); this._queue = []; if ((base = this.options).port == null) { base.port = this.options.localPort; } return this; } WSServer.prototype.start = function() { this.log('start'); this.wss = new ws.Server(this.options); this.wss.on('listening', this.onWSServerListening); this.wss.on('connection', this.onWSServerConnection); return this.wss.on('error', this.onWSServerError); }; WSServer.prototype.isActive = function() { return (this.wss != null) && (this.ws != null) && this.ready; }; WSServer.prototype._enqueue = function(data) { return this._queue.push(data); }; WSServer.prototype.send = function(data) { if (!this.isActive()) { return this._enqueue(data); } else { this.log('send', data); return this.ws.send(data); } }; WSServer.prototype.end = function(fn) { if (fn == null) { fn = null; } this.log('end'); if (this.isActive()) { this.ws.close(); } this.wss = null; this.ws = null; this.ready = false; if (fn) { return fn(); } }; WSServer.prototype.onWSServerListening = function() { this.log('onWSServerListening'); return this.emit('listening'); }; WSServer.prototype.onWSServerConnection = function(socket) { var data, i, len, ref, results; this.log('onWSServerConnection'); this.emit('connection'); this.ws = socket; this.ws.on('open', this.onClientOpen); this.ws.on('close', this.onClientClose); this.ws.on('error', this.onClientError); this.ws.on(this.options.emitKey, this.onClientMessage); this.ws.on('connect', this.onClientConnect); this.ready = true; ref = this._queue; results = []; for (i = 0, len = ref.length; i < len; i++) { data = ref[i]; results.push(this.send(data)); } return results; }; WSServer.prototype.onWSServerError = function(err) { this.log('onWSServerError', err); return this.emit('error', err); }; WSServer.prototype.onClientConnect = function() { this.log('onClientConnect'); return this.emit('connect'); }; WSServer.prototype.onClientOpen = function() { this.log('onClientOpen'); return this.emit('open'); }; WSServer.prototype.onClientClose = function() { this.log('onClientClose'); return this.emit('close'); }; WSServer.prototype.onClientError = function(err) { this.log('onClientError', err); return this.emit('error', err); }; WSServer.prototype.onClientMessage = function(msg) { this.log('onClientMessage', msg); return this.emit(this.options.emitKey, msg); }; return WSServer; })(Base); module.exports = { WSServer: WSServer }; }).call(this);