Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Start on using http.Server instead of net.Server

  • Loading branch information...
commit 6096edebdeafdbf954b379d9d7ab773e16138bf9 1 parent f61300d
@miksago miksago authored
Showing with 64 additions and 28 deletions.
  1. +1 −1  example.js
  2. +52 −27 lib/ws.js
  3. +1 −0  lib/ws/md5.js
  4. +10 −0 sample-data.txt
View
2  example.js
@@ -12,4 +12,4 @@ server.addListener("ready", function(s){
}, 1000);
});
-server.listen(7000, '192.168.46.19');
+server.listen(7000);
View
79 lib/ws.js
@@ -1,7 +1,7 @@
// System
var sys = require("sys"),
- net = require("net"),
+ http = require("http"),
events = require("events"),
path = require("path");
@@ -13,36 +13,61 @@ require.paths.unshift(__dirname);
var Connection = require('ws/connection'),
Utils = require('ws/utilities');
-function Server(options){
- net.Server.call(this);
+exports.Server = Server;
+exports.createServer = function(){
+ return new Server();
+}
+
+function Server(handler){
+ http.Server.call(this);
- var self = this,
- address = null,
- port = null,
- options = options || {};
-
- this.addListener("listening", function(){
- var tmp = self.address();
- address = tmp.address;
- port = tmp.port;
- });
+ this.connection = {};
+ this.clients = [];
- this.addListener("connection", function(socket){
- socket.host = address;
- socket.port = port;
- options.origin = options.origin || "*";
- var handler = new Connection(socket, options);
- });
+ if(handler){
+ this.noHandler = handler;
+ }
- this.addListener("error", function(e){
- Utils.log("ERROR: "+e);
+ this.addListener("listening", this.handleConnection);
+ this.addListener("request", this.handleRequest);
+};
+
+sys.inherits(Server, http.Server);
+
+Server.prototype.handleConnection = function(){
+ var tmp = this.address();
+ this.connection["address"] = tmp.address;
+ this.connection["port"] = tmp.port;
+};
+
+Server.prototype.handleRequest = function(req, res){
+ if(req.upgrade && res === null){
+ this.dispatch_ws(req);
+ } else {
+ this.noHandler(req, res);
+ }
+};
+
+Server.prototype.noHandler = function(req, res){
+ res.writeHead("501", {
+ "Content-Length": 578,
+ "Content-Type": "text/html"
});
- this.addListener("close", function(e){
- Utils.log("CLOSED");
- });
+ res.write([
+ "<!DOCTYPE html><html><head><title>501: Not Implemented</title></head><body>",
+ "<div style=\"width: 40em; color: #444!important; font-family: sans-serif; line-height: 1.4em; margin: 3em;\">",
+ "<h1>501: Not Implemented</h1>",
+ "<p>The thingy you wanted to talk to didn't get setup to talk back,<br /> deepest apologies.</p>",
+ "<hr style=\"margin: 0 -1em; border: 1px solid #aaa!important; border-width: 1px 0 0 0!important; background: #aaa!important;\"/>",
+ "<p><small>This thingy is brought to you with the help of <a href=\"http://nodejs.org/\" title=\"Project Website\">Node.js</a>.</p>",
+ "</div>",
+ "</body></html>"].join("")
+ );
+
+ res.end();
};
-sys.inherits(Server, net.Server);
-exports.Server = Server;
-
+Server.prototype.dispatch_ws = function(req){
+
+};
View
1  lib/ws/md5.js
@@ -0,0 +1 @@
+exports.decode = function(){};
View
10 sample-data.txt
@@ -50,3 +50,13 @@ WjN}|M(6
String.fromCharCode(829309203>>>16, 829309203 & 0xFF, 259970620>>>16, 259970620&0xFF) + ^n:ds[4U
+
+
+GET / HTTP/1.1
+Host: example.com
+Sec-WebSocket-Key1: 3e6b263 4 17 80
+Origin: http://example.com
+Sec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90
+
+WjN}|M(6
+
Please sign in to comment.
Something went wrong with that request. Please try again.