Permalink
Browse files

replaced all server-side references to socket closure in its callback…

…s with this
  • Loading branch information...
Montana Rowe
Montana Rowe committed Feb 22, 2012
1 parent 9b218bf commit 4dc31d6953edcc35dc458bbda7fe0b4117b53c2a
Showing with 33 additions and 16 deletions.
  1. +3 −1 chat_app/public/index.html
  2. +30 −15 chat_app/server.js
@@ -11,7 +11,9 @@
<script type="text/javascript" src="http://localhost:1337/socket.io/socket.io.js"></script>
<script type="text/javascript">
-var socket = io.connect("http://localhost:1337");
+var sioPort = 1337;
+
+var socket = io.connect("http://localhost:" + sioPort);
$(document).on("ready",function(){
//page setup
View
@@ -1,46 +1,61 @@
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 staticPort = 8000;
+var sioPort = 1337;
+
+staticPort = 15215;
+sioPort = 15216;
+
+//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 users = {};
-io.sockets.on("connection", function(socket){
+io.sockets.on("connection", function handleSocketConnect(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.");
+ this.broadcast.emit("announcement", name + " connected.");
io.sockets.emit("users",users);
}
- socket.emit("nameset",name);
+ this.emit("nameset",name);
});
socket.on("getUsers",function(){
- socket.emit("users",users);
+ this.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])
+ if(this.name && users[this.name])
{
- delete users[socket.name];
+ delete users[this.name];
}
- socket.broadcast.emit("announcement", socket.name + " disconnected.");
+ this.broadcast.emit("announcement", this.name + " disconnected.");
io.sockets.emit("users",users);
});
});

0 comments on commit 4dc31d6

Please sign in to comment.