iocat
Version:
WebSocket netcat with Socket.io support
86 lines (69 loc) • 2.5 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var Base, WSClient, 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');
WSClient = (function(superClass) {
extend(WSClient, superClass);
function WSClient(url, options) {
this.url = url;
this.options = options != null ? options : {};
this.onMessage = bind(this.onMessage, this);
this.onError = bind(this.onError, this);
this.onClose = bind(this.onClose, this);
this.onOpen = bind(this.onOpen, this);
this.onConnect = bind(this.onConnect, this);
this.end = bind(this.end, this);
this.send = bind(this.send, this);
this.start = bind(this.start, this);
this.log("constructor");
return this;
}
WSClient.prototype.start = function() {
var href;
this.log('WSClient: url->', this.url.format());
href = this.url.format();
this.ws = new ws(this.url.format());
this.ws.on('open', this.onOpen);
this.ws.on('close', this.onClose);
this.ws.on('error', this.onError);
this.ws.on(this.options.emitKey, this.onMessage);
return this.ws.on('connect', this.onConnect);
};
WSClient.prototype.send = function(d) {
this.log('send', d);
return this.ws.send(d);
};
WSClient.prototype.end = function() {
this.log('end');
return this.ws.close();
};
WSClient.prototype.onConnect = function() {
this.log('onConnect');
return this.emit('connect');
};
WSClient.prototype.onOpen = function() {
this.log('onOpen');
return this.emit('open');
};
WSClient.prototype.onClose = function() {
this.log('onClose');
return this.emit('close');
};
WSClient.prototype.onError = function(err) {
this.log('onError', err);
return this.emit('error', err);
};
WSClient.prototype.onMessage = function(msg) {
this.log('onMessage', msg);
return this.emit(this.options.emitKey, msg);
};
return WSClient;
})(Base);
module.exports = {
WSClient: WSClient
};
}).call(this);