Skip to content

Commit

Permalink
Update code to use socket.io 0.7 API.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdavey committed Jul 20, 2011
1 parent b6b6d66 commit 72a1c6a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
11 changes: 4 additions & 7 deletions src/VogueClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ function VogueClient(clientSocket, watcher) {
this.socket = clientSocket;
this.watcher = watcher;
this.watchedFiles = {};
clientSocket.on('message', this.handleMessage.bind(this));
clientSocket.on('watch', this.handleMessage.bind(this));
clientSocket.on('disconnect', this.disconnect.bind(this));
}

// Parse an incoming message from the client and dispatch accordingly.
VogueClient.prototype.handleMessage = function(message) {
var match = message.match(/^watch (.*)$/);
if (!match) return;
var href = match[1];
this.watchFile(href);
VogueClient.prototype.handleMessage = function(data) {
this.watchFile(data.href);
};

VogueClient.prototype.watchFile = function(href) {
Expand Down Expand Up @@ -46,7 +43,7 @@ VogueClient.prototype.updateFile = function(filename) {
// Only send message to client if the file was modified
// since we last saw it.
if (fileInfo.mtime < stats.mtime) {
this.socket.send('update ' + fileInfo.href);
this.socket.emit('update', { href: fileInfo.href });
fileInfo.mtime = stats.mtime;
}
}.bind(this));
Expand Down
13 changes: 4 additions & 9 deletions src/client/vogue-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@ loadScripts({
function vogue() {
window.WEB_SOCKET_SWF_LOCATION = script.url + 'socket.io/lib/vendor/web-socket-js/WebSocketMain.swf';
var stylesheets = getLocalStylesheets();
var socket = new io.Socket(script.domain, { port: script.port });
var socket = io.connect("http://" + script.domain + ":" + script.port);
socket.on('connect', watchAllStylesheets);
socket.on('message', handleMessage);
socket.connect();
socket.on('update', handleMessage);

function watchAllStylesheets() {
for (var href in stylesheets) {
socket.send('watch ' + href);
socket.emit('watch', { href: href });
}
}

function handleMessage(message) {
var match = message.match(/^update (.*)$/);
if (match) {
var href = match[1];
reloadStylesheet(href);
}
reloadStylesheet(message.href);
}

function reloadStylesheet(href) {
Expand Down
2 changes: 1 addition & 1 deletion src/vogue.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var options = getOptions()
, watcher = new Watcher(options.webDirectory,options.rewrite);

server.listen(options.port);
socket.on('connection', function(clientSocket) {
socket.sockets.on('connection', function(clientSocket) {
watcher.addClient(new VogueClient(clientSocket, watcher));
});

Expand Down

0 comments on commit 72a1c6a

Please sign in to comment.