Permalink
Browse files

Include snapshot of miksago/node-websocket-server.

- Include a static snapshot of miksago/node-websocket-server with some
  fixes applied to v1.20.000.
  • Loading branch information...
1 parent eafc37b commit 7d2293acd063a853c54d2b2e79ad6160ca7fefef @pgriess pgriess committed Jul 7, 2010
Showing with 626 additions and 4 deletions.
  1. +1 −2 lib/webworker-child.js
  2. +1 −2 lib/webworker.js
  3. +118 −0 lib/ws.js
  4. +408 −0 lib/ws/connection.js
  5. +98 −0 lib/ws/manager.js
View
@@ -20,11 +20,10 @@ var sys = require('sys');
var wwutil = require('./webworker-util');
try {
- var WebSocketServer = require('websocket-server/ws').WebSocketServer;
+ var WebSocketServer = require('./ws').WebSocketServer;
var WebSocket = require('websocket-client/websocket').WebSocket;
} catch (e) {
throw new Error(
- 'Both miksago/node-websocket-server and ' +
'pgriess/node-websocket-client must be installed'
);
}
View
@@ -38,11 +38,10 @@ var sys = require('sys');
var wwutil = require('./webworker-util');
try {
- var WebSocketServer = require('websocket-server/ws').Server;
+ var WebSocketServer = require('./ws').Server;
var WebSocket = require('websocket-client/websocket').WebSocket;
} catch (e) {
throw new Error(
- 'Both miksago/node-websocket-server and ' +
'pgriess/node-websocket-client must be installed'
);
}
View
118 lib/ws.js
@@ -0,0 +1,118 @@
+
+
+/*-----------------------------------------------
+ Requirements:
+-----------------------------------------------*/
+
+// System
+var sys = require("sys")
+ , http = require("http")
+ , events = require("events")
+ , path = require("path");
+
+// Local
+require.paths.unshift(__dirname);
+
+var Manager = require("ws/manager")
+ , Connection = require("ws/connection");
+
+
+/*-----------------------------------------------
+ Mixin:
+-----------------------------------------------*/
+var mixin = function(target, source) {
+ for(var i = 0, keys = Object.keys(source), l = keys.length; i < l; ++i) {
+ var key = keys[i];
+ target[key] = source[key];
+ }
+ return target;
+};
+
+/*-----------------------------------------------
+ WebSocket Server Exports:
+-----------------------------------------------*/
+exports.Server = Server;
+exports.createServer = function(options){
+ return new Server(options || {});
+};
+
+/*-----------------------------------------------
+ WebSocket Server Implementation:
+-----------------------------------------------*/
+function Server(options){
+ this.options = mixin({
+ debug: false, // Boolean: Show debug information.
+ version: "auto", // String: Value must be either: draft75, draft76, auto
+ origin: "*", // String, Array: A match for a valid connection origin
+ subprotocol: null, // String, Array: A match for a valid connection subprotocol.
+ }, options || {});
+
+ var ws = this;
+ this.debug = !!this.options.debug;
+ this.server = new http.Server();
+ this.manager = new Manager(this.debug);
+
+ events.EventEmitter.call(this);
+
+ this.server.addListener("upgrade", function(req, socket, upgradeHead){
+ if( req.method == "GET" && ( "upgrade" in req.headers && "connection" in req.headers) &&
+ req.headers.upgrade.toLowerCase() == "websocket" && req.headers.connection.toLowerCase() == "upgrade"
+ ){
+ // create a new connection, it'll handle everything else.
+ new Connection(ws, req, socket, upgradeHead);
+ } else {
+ // Close the socket, it wasn't a valid connection.
+ socket.end();
+ socket.destroy();
+ }
+ });
+
+ this.server.addListener("listening", function(req, res){
+ ws.emit("listening");
+ });
+
+ this.server.addListener("request", function(req, res){
+ ws.emit("request", req, res);
+ });
+
+ this.server.addListener("stream", function(stream){
+ ws.emit("stream", stream);
+ });
+
+ this.server.addListener("close", function(errno){
+ ws.emit("shutdown", errno);
+ });
+
+ this.server.addListener("clientError", function(e){
+ ws.emit("clientError", e);
+ });
+};
+
+sys.inherits(Server, events.EventEmitter);
+
+/*-----------------------------------------------
+ Public API
+-----------------------------------------------*/
+Server.prototype.listen = function(){
+ this.server.listen.apply(this.server, arguments);
+};
+
+Server.prototype.close = function(){
+ this.server.close();
+};
+
+Server.prototype.send = function(id, data){
+ this.manager.find(id, function(client){
+ if(client._state === 4){
+ client.write(data);
+ }
+ });
+};
+
+Server.prototype.broadcast = function(data){
+ this.manager.forEach(function(client){
+ if(client._state === 4){
+ client.write(data);
+ }
+ });
+};
Oops, something went wrong.

0 comments on commit 7d2293a

Please sign in to comment.