Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of /home/calcnerd256/workbench/repos/github/feb…

…2012_node_talk_chat_app into incoming

Conflicts:
	chat_app/public/index.html
  • Loading branch information...
commit f3390a11606beed9ecc2907782519009180a14f8 2 parents 7390588 + 5f65101
Montana Rowe authored
Showing with 109 additions and 40 deletions.
  1. +3 −3 chat_app/public/index.html
  2. +106 −37 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
143 chat_app/server.js
@@ -1,46 +1,115 @@
+//Contents
+// Contents
+// modules
+// configuration
+// servers
+// connect
+// socket.io
+// 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");
-});
+//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);
+ }
+);
-var io = sio.listen(1337,function(){
- console.log("socket server listening on port 1337");
-});
var users = {};
+function setUser(name){
+ var result = getUser(name);
+ users[name] = name;
+ return result;//returns the old value
+}
+function getUser(name){
+ return users[name];
+}
+function delUser(name){
+ var result = getUser(name);
+ if(name in users)
+ delete users[name];
+ return result;//returns the old value
+}
+
+
+//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);
+ }
+ this.emit("nameset", name);
+}
+MySocket.prototype.getUsers = function getUsers(){
+ this.emit("users", users);
+}
+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);
+}
-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);
- });
-});
+io.sockets.on(
+ "connection",
+ function connection(socket){
+ return new MySocket(socket);
+ }
+);
Please sign in to comment.
Something went wrong with that request. Please try again.