Skip to content

Commit

Permalink
Remove trailing whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Dec 22, 2012
1 parent 7b63baa commit ee22f75
Show file tree
Hide file tree
Showing 20 changed files with 259 additions and 259 deletions.
10 changes: 5 additions & 5 deletions examples/autobahn_client.js
Expand Up @@ -17,24 +17,24 @@ socket.onclose = function() {
if (n > cases) {
socket = new WebSocket.Client(host + '/updateReports?agent=' + encodeURIComponent(agent));
socket.onclose = process.exit

} else if (skip.indexOf(n) >= 0) {
runCase(n + 1);

} else {
console.log('Running test case #' + n + ' ...');
socket = new WebSocket.Client(host + '/runCase?case=' + n + '&agent=' + encodeURIComponent(agent));

socket.onmessage = function(event) {
socket.send(event.data);
};

socket.onclose = function() {
runCase(n + 1);
};
}
};

runCase(1);
};

16 changes: 8 additions & 8 deletions examples/server.js
Expand Up @@ -9,11 +9,11 @@ var port = process.argv[2] || 7000,
var upgradeHandler = function(request, socket, head) {
var ws = new WebSocket(request, socket, head, ['irc', 'xmpp'], {ping: 5});
console.log('open', ws.url, ws.version, ws.protocol);

ws.onmessage = function(event) {
ws.send(event.data);
};

ws.onclose = function(event) {
console.log('close', event.code, event.reason);
ws = null;
Expand All @@ -23,22 +23,22 @@ var upgradeHandler = function(request, socket, head) {
var requestHandler = function(request, response) {
if (!WebSocket.EventSource.isEventSource(request))
return staticHandler(request, response);

var es = new WebSocket.EventSource(request, response),
time = parseInt(es.lastEventId, 10) || 0;

console.log('open', es.url, es.lastEventId);

var loop = setInterval(function() {
time += 1;
es.send('Time: ' + time);
setTimeout(function() {
if (es) es.send('Update!!', {event: 'update', id: time});
}, 1000);
}, 2000);

es.send('Welcome!\n\nThis is an EventSource server.');

es.onclose = function() {
clearInterval(loop);
console.log('close', es.url);
Expand All @@ -48,7 +48,7 @@ var requestHandler = function(request, response) {

var staticHandler = function(request, response) {
var path = request.url;

fs.readFile(__dirname + path, function(err, content) {
var status = err ? 404 : 200;
response.writeHead(status, {'Content-Type': 'text/html'});
Expand Down
16 changes: 8 additions & 8 deletions examples/sse.html
Expand Up @@ -5,35 +5,35 @@
<title>EventSource test</title>
</head>
<body>

<h1>EventSource test</h1>
<ul></ul>

<script type="text/javascript">
var logger = document.getElementsByTagName('ul')[0],
socket = new EventSource('/');

var log = function(text) {
logger.innerHTML += '<li>' + text + '</li>';
};

socket.onopen = function() {
log('OPEN');
};

socket.onmessage = function(event) {
log('MESSAGE: ' + event.data);
};

socket.addEventListener('update', function(event) {
log('UPDATE(' + event.lastEventId + '): ' + event.data);
});

socket.onerror = function(event) {
log('ERROR: ' + event.message);
};
</script>

</body>
</html>

16 changes: 8 additions & 8 deletions examples/ws.html
Expand Up @@ -5,40 +5,40 @@
<title>WebSocket test</title>
</head>
<body>

<h1>WebSocket test</h1>
<ul></ul>

<script type="text/javascript">
var logger = document.getElementsByTagName('ul')[0],
Socket = window.MozWebSocket || window.WebSocket,
protos = ['foo', 'bar', 'xmpp'],
socket = new Socket('ws://' + location.hostname + ':' + location.port + '/', protos),
index = 0;

var log = function(text) {
logger.innerHTML += '<li>' + text + '</li>';
};

socket.addEventListener('open', function() {
log('OPEN: ' + socket.protocol);
socket.send('Hello, world');
});

socket.onerror = function(event) {
log('ERROR: ' + event.message);
};

socket.onmessage = function(event) {
log('MESSAGE: ' + event.data);
setTimeout(function() { socket.send(++index + ' ' + event.data) }, 2000);
};

socket.onclose = function(event) {
log('CLOSE: ' + event.code + ', ' + event.reason);
};
</script>

</body>
</html>

38 changes: 19 additions & 19 deletions lib/faye/eventsource.js
Expand Up @@ -12,42 +12,42 @@ var isSecureConnection = function(request) {

var EventSource = function(request, response, options) {
options = options || {};

this._request = request;
this._response = response;
this._stream = response.socket;
this._ping = options.ping || this.DEFAULT_PING;
this._retry = options.retry || this.DEFAULT_RETRY;

var scheme = isSecureConnection(request) ? 'https:' : 'http:';
this.url = scheme + '//' + request.headers.host + request.url;

this.lastEventId = request.headers['last-event-id'] || '';

var self = this;
this.readyState = API.CONNECTING;
this._sendBuffer = [];
process.nextTick(function() { self._open() });

var handshake = 'HTTP/1.1 200 OK\r\n' +
'Content-Type: text/event-stream\r\n' +
'Cache-Control: no-cache, no-store\r\n' +
'Connection: close\r\n' +
'\r\n\r\n' +
'retry: ' + Math.floor(this._retry * 1000) + '\r\n\r\n';

this.readyState = API.OPEN;

if (this._ping)
this._pingLoop = setInterval(function() { self.ping() }, this._ping * 1000);

if (!this._stream || !this._stream.writable) return;

this._stream.setTimeout(0);
this._stream.setNoDelay(true);

try { this._stream.write(handshake, 'utf8') } catch (e) {}

['close', 'end', 'error'].forEach(function(event) {
self._stream.addListener(event, function() { self.close() });
});
Expand All @@ -61,26 +61,26 @@ EventSource.isEventSource = function(request) {
var instance = {
DEFAULT_PING: 10,
DEFAULT_RETRY: 5,

send: function(message, options) {
if (this.readyState !== API.OPEN) return false;

message = String(message).replace(/(\r\n|\r|\n)/g, '$1data: ');
options = options || {};

var frame = '';
if (options.event) frame += 'event: ' + options.event + '\r\n';
if (options.id) frame += 'id: ' + options.id + '\r\n';
frame += 'data: ' + message + '\r\n\r\n';

try {
this._stream.write(frame, 'utf8');
return true;
} catch (e) {
return false;
}
},

ping: function() {
try {
this._stream.write(':\r\n\r\n', 'utf8');
Expand All @@ -89,15 +89,15 @@ var instance = {
return false;
}
},

close: function() {
if (this.readyState === API.CLOSING || this.readyState === API.CLOSED)
return;

this.readyState = API.CLOSED;
clearInterval(this._pingLoop);
this._response.end();

var event = new Event('close');
event.initEvent('close', false, false);
this.dispatchEvent(event);
Expand Down
20 changes: 10 additions & 10 deletions lib/faye/websocket.js
Expand Up @@ -36,38 +36,38 @@ var WebSocket = function(request, socket, head, supportedProtos, options) {
this._stream = request.socket;
this._ping = options && options.ping;
this._pingId = 0;

var scheme = isSecureConnection(request) ? 'wss:' : 'ws:';
this.url = scheme + '//' + request.headers.host + request.url;
this.readyState = API.CONNECTING;
this.bufferedAmount = 0;

var Parser = getParser(request);
this._parser = new Parser(this, {protocols: supportedProtos});

var self = this;
this._sendBuffer = [];
process.nextTick(function() { self._open() });

var handshake = this._parser.handshakeResponse(head);
if (this._parser.isOpen()) this.readyState = API.OPEN;

if (this._ping)
this._pingLoop = setInterval(function() {
self._pingId += 1;
self.ping(self._pingId.toString());
}, this._ping * 1000);

this.protocol = this._parser.protocol || '';
this.version = this._parser.getVersion();

if (!this._stream || !this._stream.writable) return;

this._stream.setTimeout(0);
this._stream.setNoDelay(true);

try { this._stream.write(handshake, 'binary') } catch (e) {}

this._stream.addListener('data', function(data) {
var response = self._parser.parse(data);
if (!response) return;
Expand Down
20 changes: 10 additions & 10 deletions lib/faye/websocket/api.js
Expand Up @@ -6,30 +6,30 @@ var API = {
OPEN: 1,
CLOSING: 2,
CLOSED: 3,

_open: function() {
if (this._parser && !this._parser.isOpen()) return;
this.readyState = API.OPEN;

var buffer = this._sendBuffer || [],
message;

while (message = buffer.shift())
this.send.apply(this, message);

var event = new Event('open');
event.initEvent('open', false, false);
this.dispatchEvent(event);
},

receive: function(data) {
if (this.readyState !== API.OPEN) return false;
var event = new Event('message');
event.initEvent('message', false, false);
event.data = data;
this.dispatchEvent(event);
},

send: function(data, type, errorType) {
if (this.readyState === API.CONNECTING) {
if (this._sendBuffer) {
Expand All @@ -39,12 +39,12 @@ var API = {
throw new Error('Cannot call send(), socket is not open yet');
}
}

if (this.readyState === API.CLOSED)
return false;

if (!(data instanceof Buffer)) data = String(data);

var frame = this._parser.frame(data, type, errorType);
try {
this._stream.write(frame, 'binary');
Expand All @@ -53,7 +53,7 @@ var API = {
return false;
}
},

close: function(code, reason, ack) {
if (this.readyState === API.CLOSING ||
this.readyState === API.CLOSED) return;
Expand Down

0 comments on commit ee22f75

Please sign in to comment.