Browse files

Merge pull request #1 from calcnerd256/master

server module overhaul
  • Loading branch information...
2 parents 7390588 + 2af4a6a commit 297930bb477362f8d933f019fb48a20055292adb @vantreeseba vantreeseba committed Jan 18, 2013
Showing with 137 additions and 46 deletions.
  1. +3 −3 chat_app/public/index.html
  2. +134 −43 chat_app/server.js
View
6 chat_app/public/index.html
@@ -11,8 +11,8 @@
<script type="text/javascript" src="http://okcjs.ideasrun.com:1337/socket.io/socket.io.js"></script>
<script type="text/javascript">
-var socket = io.connect("http://okcjs.ideasrun.com:1337");
-
+var sioPort = 1337;
+var socket = io.connect("http://okcjs.ideasrun.com:" + sioPort);
$(document).on("ready",function(){
//page setup
$("#chat").hide(); //hide this till the username is set.
@@ -47,7 +47,7 @@
alert("name is in use");
} else {
$("#name-inputs").dialog("close");
- $("#chat").show();
+ $("#chat").show();
$("#users").show();
$("#message").focus();
}
View
177 chat_app/server.js
@@ -1,46 +1,137 @@
+//Contents
+// Contents
+// modules
+// configuration
+// servers
+// connect
+// socket.io
+// PrivateCell
+// users
+// setUser
+// getUser
+// delUser
+// MySocket
+// events
+// listener
+// setname
+// getUsers
+// message
+// disconnect
+// socket connection listener
+
+
+
+//modules
var connect = require("connect");
var sio = require("socket.io");
-connect(connect.static(__dirname + "/public")).listen(8000, function(){
- console.log("static server listening on port 8000");
-});
-
-
-var io = sio.listen(1337,function(){
- console.log("socket server listening on port 1337");
-});
-
-var users = {};
-
-
-io.sockets.on("connection", function(socket){
-
- socket.on("setname",function(name,fn){
- if(users[name]){
- fn(true);
- } else {
- fn(false);
- users[name] = socket.name = name;
- socket.broadcast.emit("announcement", name + " connected.");
- io.sockets.emit("users",users);
- }
- socket.emit("nameset",name);
- });
-
- socket.on("getUsers",function(){
- socket.emit("users",users);
- });
-
- socket.on("message",function(message){
- io.sockets.emit("message", socket.name + ": " + message);
- });
-
- socket.on("disconnect",function(){
- if(socket.name && users[socket.name])
- {
- delete users[socket.name];
- }
- socket.broadcast.emit("announcement", socket.name + " disconnected.");
- io.sockets.emit("users",users);
- });
-});
+//configuration
+var staticPort = 8000;
+var sioPort = 1337;
+
+//servers
+
+//the connect module acts as a factory function that takes middleware arguments
+connect(
+ connect.static(__dirname + "/public")
+).listen(
+ staticPort,
+ function(){
+ console.log("static server listening on port " + staticPort);
+ }
+);
+
+var io = sio.listen(
+ sioPort,
+ function(){
+ console.log("socket server listening on port " + sioPort);
+ }
+);
+
+
+function PrivateCell(val){
+ var value = {val: val};
+ function get(){
+ return value.val;
+ }
+ function getKey(key){
+ return get()[key];
+ }
+ function set(val){
+ var result = get();
+ value.val = val;
+ return result;
+ }
+ function setKey(key, val){
+ var result = getKey(key);
+ get()[key] = val;
+ return result;
+ }
+ function delKey(key){
+ var result = getKey(key);
+ var got = get();
+ if(key in got)
+ delete got[key];
+ return result;
+ }
+ this.set = set;
+ this.get = get;
+ this.getKey = getKey;
+ this.setKey = setKey;
+ this.delKey = delKey;
+}
+var users = new PrivateCell({});
+var setUser = function(name){return users.setKey(name, name)};
+var getUser = users.getKey;
+var delUser = users.delKey;
+delete users.set;
+
+
+//a class for our sockets
+function MySocket(socket){
+ this.socket = socket;
+ for(var i = 0; i < this.events.length; i++)
+ this.listenOn(this.events[i]);
+}
+MySocket.prototype.events = [
+ "setname",
+ "getUsers",
+ "message",
+ "disconnect"
+];
+MySocket.prototype.listenOn = function listenOn(name){
+ this.socket.on(name, this[name]);
+}
+MySocket.prototype.setname = function setname(name, fn){
+ if(getUser(name)){
+ fn(true);
+ } else {
+ fn(false);
+ setUser(this.name = name);
+ this.broadcast.emit("announcement", name + " connected.");
+ io.sockets.emit("users", users.get());
+ }
+ this.emit("nameset", name);
+}
+MySocket.prototype.getUsers = function getUsers(){
+ this.emit("users", users.get());
+}
+MySocket.prototype.message = function message(message){
+ io.sockets.emit("message", this.name + ": " + message);
+}
+MySocket.prototype.disconnect = function disconnect(){
+ if(this.name)
+ {
+ delUser(this.name);
+ }
+ this.broadcast.emit("announcement", this.name + " disconnected.");
+ io.sockets.emit("users", users.get());
+}
+
+
+io.sockets.on(
+ "connection",
+ function connection(socket){
+ return new MySocket(socket);
+ }
+);

0 comments on commit 297930b

Please sign in to comment.