Permalink
Browse files

Starting the addition of documentation and examples.

  • Loading branch information...
1 parent 99210fd commit c381e930561f3408a8971280f0d9e57ee7e93ca8 @miksago miksago committed May 9, 2010
Showing with 75 additions and 7 deletions.
  1. +21 −0 LICENSE.md
  2. +43 −0 README.md
  3. +1 −2 example.html
  4. +10 −5 server.example.js
View
@@ -0,0 +1,21 @@
+Copyright 2010 Micheil Smith.
+
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
View
@@ -0,0 +1,43 @@
+# node-websocket-server #
+
+**IMPORTANT: ** This module only works with node v0.1.94 and later.
+
+This is a server for the WebSocket Protocol. It is designed to work
+with both [draft75](http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75) and [draft76 or later](http://www.whatwg.org/specs/web-socket-protocol/) of the protocol specification.
@xantus

xantus Jun 7, 2010

Correction, it does not implement draft76

@ThisIsMissEm

ThisIsMissEm Jun 7, 2010

Owner

Correction: It is designed to work with both draft76 and 75, I just haven't written 76 support, as at the time, 76 was still fairly alpha.

@xantus

xantus Jun 8, 2010

Dev bulids of webkit and chrome both have moved to draft76

@ThisIsMissEm

ThisIsMissEm Jun 8, 2010

Owner

To be rather frank, I don't yet have time to write the code to be a spec compliant upgrade to draft76 handshaking, as it'll require about 6-48hrs of work (for reasons of speed, efficiency, and testing). In the mean time, rather then complaining that the websocket modules don't work with browser XYZ or draft version X, why not write a patch for it?

Also, the major webkit browser that's directly webkit that was released today uses draft75; Only chromium uses it fulltime in production atm, I believe chrome is some where in between. As for firefox, I have no idea where it's at.

+
+## Synopsis ##
+
+An example of a simple server that will echo the messages received back out.
+
+ var sys = require("sys");
+ var ws = require('./lib/ws');
+
+ var server = ws.createServer();
+ server.listen(8000);
+
+ server.addListener("listening", function(){
+ sys.log("Listening for connections.");
+ });
+
+ function braodcast(server, conn, data){
+ for(var cid in server.connections){
+ server.connections[cid].write("<"+conn._id+"> "+data);
+ }
+ };
+
+ server.addListener("connection", function(conn){
+ sys.log("<"+conn._id+"> connected");
+ braodcast(server, conn, "connected");
+
+ conn.addListener("close", function(){
+ sys.log("<"+conn._id+"> onClose");
+ braodcast(server, conn, "disconnected");
+ });
+
+ conn.addListener("message", function(message){
+ sys.log("<"+conn._id + "> "+message);
+ braodcast(server, conn, message);
+ });
+ });
+
+Coupled with a websocket client like the `example.html`, and you have a working websocket chat client (sort of.)
View
@@ -29,11 +29,10 @@
}
var output_log = document.getElementById("log");
- var x = 0;
conn.onmessage = function(evt){
console.log(evt.data);
var msg = document.createElement("p");
- msg.innerHTML = (x++)+""+evt.data;
+ msg.innerHTML = evt.data;
output_log.appendChild(msg);
};
View
@@ -7,25 +7,30 @@ function log(data){
sys.log("\033[0;32m"+data.toString()+"\033[0m");
};
-
var server = ws.createServer();
-
server.listen(8000);
server.addListener("listening", function(){
log("Listening for connections.");
});
+function braodcast(server, conn, data){
+ for(var cid in server.connections){
+ server.connections[cid].write("<"+conn._id+"> "+data);
+ }
+};
+
server.addListener("connection", function(conn){
- log("<"+conn._id+"> new connection");
- conn.write(logger.timestamp()+" new connection");
+ log("<"+conn._id+"> connected");
+ braodcast(server, conn, "connected");
conn.addListener("close", function(){
log("<"+conn._id+"> onClose");
+ braodcast(server, conn, "disconnected");
});
conn.addListener("message", function(message){
log("<"+conn._id + "> "+message);
- conn.write(logger.timestamp()+" "+message);
+ braodcast(server, conn, message);
});
});

0 comments on commit c381e93

Please sign in to comment.