2 var util = require('util');
3 var events = require('events');
4 var WebSocket = require('faye-websocket');
6 module.exports = Client;
8 function Client(req, socket, head, options) {
9 options = this.options = options || {};
10 this.ws = new WebSocket(req, socket, head);
11 this.ws.onmessage = this.message.bind(this);
12 this.ws.onclose = this.close.bind(this);
13 this.id = this.uniqueId('ws');
16 util.inherits(Client, events.EventEmitter);
18 Client.prototype.message = function message(event) {
19 var data = this.data(event);
20 if(this[data.command]) return this[data.command](data);
23 Client.prototype.close = function close(event) {
29 this.emit('end', event);
34 Client.prototype.hello = function hello() {
38 'http://livereload.com/protocols/official-7'
44 Client.prototype.info = function info(data) {
45 this.plugins = data.plugins;
51 Client.prototype.reload = function reload(files) {
52 files.forEach(function(file) {
56 liveCSS: this.options.liveCSS !== false,
57 liveJs: this.options.liveJs !== false,
58 liveImg: this.options.liveImg !== false
65 Client.prototype.data = function _data(event) {
68 data = JSON.parse(event.data);
73 Client.prototype.send = function send(data) {
74 this.ws.send(JSON.stringify(data));
78 Client.prototype.uniqueId = function uniqueId(prefix) {
80 return prefix ? prefix + id : id;