Permalink
Browse files

Extract Node WebSocket code.

  • Loading branch information...
1 parent aef0d1e commit 41bafd6247a1c73164f134609734aa58105bdf47 @jcoglan jcoglan committed Nov 28, 2011
@@ -1,41 +0,0 @@
-var faye = require('../../../build/faye-node');
-
-var host = 'ws://localhost:9001',
- agent = 'Faye (Node ' + process.version + ')',
- cases = 0,
- skip = [247,248,249,250,251,252,253,254,255,
- 256,257,258,259,260,261,262,263,264];
-
-var socket = new faye.WebSocket.Client(host + '/getCaseCount');
-
-socket.onmessage = function(event) {
- console.log('Total cases to run: ' + event.data);
- cases = parseInt(event.data);
-};
-
-socket.onclose = function() {
- var runCase = function(n) {
- if (n > cases) {
- socket = new faye.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 faye.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);
-};
-
@@ -1,18 +0,0 @@
-var faye = require('../../../build/faye-node'),
- port = process.argv[2] || 7000,
- socket = new faye.WebSocket.Client('ws://localhost:' + port + '/');
-
-socket.onopen = function(event) {
- console.log('open');
- socket.send(JSON.stringify({channel: '/meta/subscribe', subscription: '/foo'}));
-};
-
-socket.onmessage = function(event) {
- console.log('message', event.data);
- // socket.close(1002, 'Going away');
-};
-
-socket.onclose = function(event) {
- console.log('close', event.code, event.reason);
-};
-
@@ -1,20 +0,0 @@
-var faye = require('../../../build/faye-node'),
- http = require('http');
-
-var port = process.argv[2] || 7000;
-
-var server = http.createServer();
-server.addListener('upgrade', function(request, socket, head) {
- var socket = new faye.WebSocket(request, socket, head);
-
- socket.onmessage = function(event) {
- socket.send(event.data);
- };
-
- socket.onclose = function(event) {
- console.log('close', event.code, event.reason);
- socket = null;
- };
-});
-
-server.listen(port);
View
@@ -33,12 +33,6 @@ packages:
extends: core
files:
- util/set
- - util/node/web_socket
- - util/node/web_socket/client
- - util/node/web_socket/api
- - util/node/web_socket/draft75_parser
- - util/node/web_socket/draft76_parser
- - util/node/web_socket/protocol8_parser
- engines/base
- engines/connection
- engines/memory
@@ -8,6 +8,8 @@ var crypto = require('crypto'),
url = require('url'),
querystring = require('querystring');
+Faye.WebSocket = require('faye-websocket');
+
Faye.logger = function(message) {
console.log(message);
};
@@ -1,61 +0,0 @@
-/**
- * For implementation reference:
- * http://dev.w3.org/html5/websockets/
- * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
- * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
- * http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10
- * http://www.w3.org/TR/DOM-Level-2-Events/events.html
- **/
-
-Faye.WebSocket = Faye.Class({
- initialize: function(request, socket, head) {
- this.request = request;
- this._stream = request.socket;
-
- var scheme = Faye.WebSocket.isSecureConnection(request) ? 'wss:' : 'ws:';
- this.url = scheme + '//' + request.headers.host + request.url;
- this.readyState = Faye.WebSocket.CONNECTING;
- this.bufferedAmount = 0;
-
- var Parser = Faye.WebSocket.getParser(request);
- this._parser = new Parser(this, this._stream);
- this._parser.handshakeResponse(head);
-
- this.readyState = Faye.WebSocket.OPEN;
- this.version = this._parser.version;
-
- var event = new Faye.WebSocket.Event('open');
- event.initEvent('open', false, false);
- this.dispatchEvent(event);
-
- var self = this;
-
- this._stream.addListener('data', function(data) {
- self._parser.parse(data);
- });
- this._stream.addListener('close', function() {
- self.close(1006, '', false);
- });
- this._stream.addListener('error', function() {});
- }
-});
-
-Faye.extend(Faye.WebSocket, {
- getParser: function(request) {
- var headers = request.headers;
- return headers['sec-websocket-version']
- ? this.Protocol8Parser
- : (headers['sec-websocket-key1'] && headers['sec-websocket-key2'])
- ? this.Draft76Parser
- : this.Draft75Parser;
- },
-
- isSecureConnection: function(request) {
- if (request.headers['x-forwarded-proto']) {
- return request.headers['x-forwarded-proto'] === 'https';
- } else {
- return (request.connection && request.connection.authorized !== undefined) ||
- (request.socket && request.socket.secure);
- }
- }
-});
@@ -1,91 +0,0 @@
-Faye.WebSocket.API = {
- onopen: null,
- onmessage: null,
- onerror: null,
- onclose: null,
-
- receive: function(data) {
- if (this.readyState !== Faye.WebSocket.OPEN) return false;
- var event = new Faye.WebSocket.Event('message');
- event.initEvent('message', false, false);
- event.data = data;
- this.dispatchEvent(event);
- },
-
- send: function(data, type, errorType) {
- if (this.readyState === Faye.WebSocket.CLOSED) return false;
- return this._parser.frame(data, type, errorType);
- },
-
- close: function(code, reason, ack) {
- if (this.readyState === Faye.WebSocket.CLOSING ||
- this.readyState === Faye.WebSocket.CLOSED) return;
-
- this.readyState = Faye.WebSocket.CLOSING;
-
- var close = function() {
- this.readyState = Faye.WebSocket.CLOSED;
- this._stream.end();
- var event = new Faye.WebSocket.Event('close', {code: code || 1000, reason: reason || ''});
- event.initEvent('close', false, false);
- this.dispatchEvent(event);
- };
-
- if (ack !== false) {
- if (this._parser.close) this._parser.close(code, reason, close, this);
- else close.call(this);
- } else {
- if (this._parser.close) this._parser.close(code, reason);
- close.call(this);
- }
- },
-
- addEventListener: function(type, listener, useCapture) {
- this.bind(type, listener);
- },
-
- removeEventListener: function(type, listener, useCapture) {
- this.unbind(type, listener);
- },
-
- dispatchEvent: function(event) {
- event.target = event.currentTarget = this;
- event.eventPhase = Faye.WebSocket.Event.AT_TARGET;
-
- this.trigger(event.type, event);
- if (this['on' + event.type])
- this['on' + event.type](event);
- }
-};
-
-Faye.extend(Faye.WebSocket.API, Faye.Publisher);
-Faye.extend(Faye.WebSocket.Client.prototype, Faye.WebSocket.API);
-Faye.extend(Faye.WebSocket.prototype, Faye.WebSocket.API);
-
-Faye.extend(Faye.WebSocket, {
- CONNECTING: 0,
- OPEN: 1,
- CLOSING: 2,
- CLOSED: 3,
-
- Event: Faye.extend(Faye.Class({
- initialize: function(eventType, options) {
- this.type = eventType;
- Faye.extend(this, options);
- },
-
- initEvent: function(eventType, canBubble, cancelable) {
- this.type = eventType;
- this.bubbles = canBubble;
- this.cancelable = cancelable;
- },
-
- stopPropagation: function() {},
- preventDefault: function() {}
-
- }), {
- CAPTURING_PHASE: 1,
- AT_TARGET: 2,
- BUBBLING_PHASE: 3
- })
-});
@@ -1,67 +0,0 @@
-Faye.WebSocket.Client = Faye.Class({
- initialize: function(url) {
- this.url = url;
- this.uri = require('url').parse(url);
-
- this.readyState = Faye.WebSocket.CONNECTING;
- this.bufferedAmount = 0;
-
- var secure = (this.uri.protocol === 'wss:'),
- self = this,
- onConnect = function() { self._onConnect() },
-
- connection = secure
- ? tls.connect(this.uri.port || 443, this.uri.hostname, onConnect)
- : net.createConnection(this.uri.port || 80, this.uri.hostname);
-
- this._parser = new Faye.WebSocket.Protocol8Parser(this, connection, {masking: true});
- this._stream = connection;
-
- if (!secure) connection.addListener('connect', onConnect);
-
- connection.addListener('data', function(data) {
- self._onData(data);
- });
- connection.addListener('close', function() {
- self.close(1006, '', false);
- });
- connection.addListener('error', function() {});
- },
-
- _onConnect: function() {
- this._handshake = this._parser.createHandshake(this.uri);
- this._message = [];
- this._handshake.requestData();
- },
-
- _onData: function(data) {
- switch (this.readyState) {
- case Faye.WebSocket.CONNECTING:
- var bytes = this._handshake.parse(data);
- for (var i = 0, n = bytes.length; i < n; i++)
- this._message.push(bytes[i]);
-
- if (!this._handshake.isComplete()) return;
-
- if (this._handshake.isValid()) {
- this.readyState = Faye.WebSocket.OPEN;
- var event = new Faye.WebSocket.Event('open');
- event.initEvent('open', false, false);
- this.dispatchEvent(event);
-
- this._parser.parse(this._message);
-
- } else {
- this.readyState = Faye.WebSocket.CLOSED;
- var event = new Faye.WebSocket.Event('close');
- event.initEvent('close', false, false);
- this.dispatchEvent(event);
- }
- break;
-
- case Faye.WebSocket.OPEN:
- case Faye.WebSocket.CLOSING:
- this._parser.parse(data);
- }
- }
-});
@@ -1,62 +0,0 @@
-Faye.WebSocket.Draft75Parser = Faye.Class({
- FRAME_START : new Buffer([0x00]),
- FRAME_END : new Buffer([0xFF]),
-
- version : 'draft-75',
-
- initialize: function(webSocket, stream) {
- this._socket = webSocket;
- this._stream = stream;
- this._buffer = [];
- this._buffering = false;
- },
-
- handshakeResponse: function() {
- var stream = this._stream;
- try {
- stream.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n');
- stream.write('Upgrade: WebSocket\r\n');
- stream.write('Connection: Upgrade\r\n');
- stream.write('WebSocket-Origin: ' + this._socket.request.headers.origin + '\r\n');
- stream.write('WebSocket-Location: ' + this._socket.url + '\r\n\r\n');
- } catch (e) {
- // socket closed while writing
- // no handshake sent; client will stop using WebSocket
- }
- },
-
- parse: function(data) {
- for (var i = 0, n = data.length; i < n; i++)
- this._handleChar(data[i]);
- },
-
- frame: function(data) {
- var stream = this._stream;
- try {
- stream.write(this.FRAME_START, 'binary');
- stream.write(new Buffer(data), 'utf8');
- stream.write(this.FRAME_END, 'binary');
- return true;
- } catch (e) {
- return false;
- }
- },
-
- _handleChar: function(data) {
- switch (data) {
- case 0x00:
- this._buffering = true;
- break;
-
- case 0xFF:
- this._buffer = new Buffer(this._buffer);
- this._socket.receive(this._buffer.toString('utf8', 0, this._buffer.length));
- this._buffer = [];
- this._buffering = false;
- break;
-
- default:
- if (this._buffering) this._buffer.push(data);
- }
- }
-});
Oops, something went wrong.

0 comments on commit 41bafd6

Please sign in to comment.