Permalink
Browse files

move to older socket.io for faster build times and proper devDependen…

…cy support.
  • Loading branch information...
1 parent f79c546 commit 68dc5019b58b0e9f48500d97e3f8f181dd036258 @jairajs89 committed Apr 5, 2013
Showing with 116 additions and 66 deletions.
  1. +28 −14 client.js
  2. +21 −19 package.json
  3. +1 −2 run.js
  4. +66 −31 zerver.js
View
@@ -1,5 +1,3 @@
-//TODO: remove dependency on JSON
-
(function (window) {
var XHR_TIMEOUT = 30000;
@@ -243,12 +241,17 @@
if (!this.readyState || (this.readyState == 'loaded') || (this.readyState == 'complete')) {
done = true;
setTimeout(function () {
- var handlers = apiSocket.slice();
+ var socket = new io.Socket(null, {
+ transports: ['htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
+ });
- apiSocket = io.connect('//'+apiHost+'/'+apiDir+'/_refresh');
+ socket.connect(function () {
+ var handlers = apiSocket.slice();
+ apiSocket = socket;
- handlers.forEach(function (handler) {
- handler();
+ handlers.forEach(function (handler) {
+ handler();
+ });
});
}, 0);
script.onload = script.onreadystatechange = null;
@@ -268,7 +271,11 @@
}
setupSocket(function () {
- apiSocket.on('refresh', function () {
+ apiSocket.on('message', function (data) {
+ if (data.type !== 'refresh') {
+ return;
+ }
+
var refresher = window[REFRESH_FUNC];
if (typeof refresher === 'function') {
@@ -289,10 +296,14 @@
setupLoggers();
function setupCLI () {
- apiSocket.on('eval', function (line, callback) {
+ apiSocket.on('message', function (data) {
+ if (data.type !== 'eval') {
+ return;
+ }
+
var success, val, error;
try {
- val = new Function('return ' + line)();
+ val = new Function('return ' + data.line)();
success = true;
error = undefined;
}
@@ -319,10 +330,12 @@
}
}
- callback({
- error : error ,
- output : jsonVal ,
- type : type
+ apiSocket.send({
+ type : 'eval' ,
+ requestID : data.requestID ,
+ error : error ,
+ output : jsonVal ,
+ dataType : type
});
});
}
@@ -342,7 +355,8 @@
}
function pipeLog (level, message) {
- apiSocket.emit('log', {
+ apiSocket.send({
+ type : 'log' ,
level : level ,
message : message
});
View
@@ -1,25 +1,27 @@
{
- "author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
- "name": "zerver",
- "description": "client-integrated webapp server",
- "version": "0.9.7",
- "repository": {
- "type": "git",
- "url": "git://github.com/jairajs89/zerver.git"
+ "author" : "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
+ "name" : "zerver",
+ "description" : "client-integrated webapp server",
+ "version" : "0.9.8",
+ "license" : "MIT" ,
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/jairajs89/zerver.git"
},
- "main": "nodeclient.js",
- "engines": {
- "node": "0.8.x"
+ "engines" : {
+ "node" : "0.8.x"
},
- "dependencies": {
- "mime" : "1.2.7" ,
- "yuicompressor" : "2.4.7" ,
- "less" : "1.3.3" ,
- "stalker" : "0.0.20" ,
- "socket.io" : "0.9.11"
+ "dependencies" : {
+ "mime" : "1.2.7" ,
+ "yuicompressor" : "2.4.7" ,
+ "less" : "1.3.3"
},
- "bin": {
- "zerver": "run.js"
+ "devDependencies" : {
+ "stalker" : "0.0.20" ,
+ "socket.io" : "0.6.18"
},
- "license": "MIT"
+ "main" : "nodeclient.js",
+ "bin" : {
+ "zerver": "run.js"
+ }
}
View
3 run.js
@@ -276,11 +276,10 @@ function main () {
try {
require('socket.io');
- require('findit' );
require('stalker' );
}
catch (err) {
- console.error('zerver debug mode requires dev dependencies');
+ console.error('zerver debug mode requires dev dependencies to be installed');
console.error('run this command: npm install --dev zerver');
return;
}
View
@@ -1275,57 +1275,92 @@ function setupAutoRefresh () {
return;
}
- var io = require('socket.io').listen(app, { log: false }),
- sockets = io.of('/'+API_URL+'/_refresh');
+ var io = require('socket.io').listen(app, {
+ log : function () {} ,
+ flashPolicyServer : false ,
+ transports : ['htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
+ });
if (REFRESH) {
process.on('message', function (data) {
if (data && data.debugRefresh) {
- sockets.emit('refresh');
+ io.broadcast({ type : 'refresh' });
}
});
}
if (LOGGING) {
- sockets.on('connection', function (socket) {
- socket.on('log', function (data) {
- console.log(data.level + ': ' + data.message);
+ io.on('connection', function (client) {
+ client.on('message', function (data) {
+ if (data.type === 'log') {
+ console.log(data.level + ': ' + data.message);
+ }
});
});
+
process.on('message', function (data) {
- if (data && data.cli) {
- var socket = getSingleSocket();
- if (socket) {
- socket.emit('eval', data.cli, function (data) {
- if (data.type === 'string') {
- console.log(data.output);
- }
- else if (data.type === 'json') {
- try {
- console.log( JSON.parse(data.output) );
- }
- catch (err) {
- console.error('(zerver cli error)');
- }
- }
- else {
- console.error(data.error);
- }
- process.send({ prompt: true });
- });
+ if (!data || !data.cli) {
+ return;
+ }
+
+ var socket = getSingleSocket();
+
+ if ( !socket ) {
+ console.warn('(no browsers available)');
+ process.send({ prompt: true });
+ return;
+ }
+
+ var requestID = 'x'+Math.random();
+
+ socket.send({
+ type : 'eval' ,
+ line : data.cli ,
+ requestID : requestID
+ });
+
+ socket.on('message', handleMessage);
+
+ var timeout = setTimeout(function () {
+ socket.removeListener('message', handleMessage);
+ finish();
+ }, 10 * 1000);
+
+ function handleMessage (data) {
+ if ((data.type !== 'eval') || (data.requestID !== requestID)) {
+ return;
+ }
+
+ clearTimeout(timeout);
+
+ if (data.dataType === 'string') {
+ console.log(data.output);
+ }
+ else if (data.dataType === 'json') {
+ try {
+ console.log( JSON.parse(data.output) );
+ }
+ catch (err) {
+ console.error('(zerver cli error)');
+ }
}
else {
- console.warn('(no browsers available)');
- process.send({ prompt: true });
+ console.error(data.error);
}
+
+ finish();
+ }
+
+ function finish () {
+ process.send({ prompt: true });
}
});
}
function getSingleSocket () {
- for (var id in sockets.sockets) {
- if ( !sockets.sockets[id].disconnected ) {
- return sockets.sockets[id];
+ for (var id in io.clients) {
+ if ( io.clients[id].connected ) {
+ return io.clients[id];
}
}
}

0 comments on commit 68dc501

Please sign in to comment.