Permalink
Browse files

Chat example rewrite.

  • Loading branch information...
1 parent da39d4d commit 14dcd3f247a0b2a832a2b8c05ed976e40b93dcc1 @yojimbo87 committed Oct 23, 2011
Showing with 91 additions and 112 deletions.
  1. +19 −9 examples/chat/client.js
  2. +31 −31 examples/chat/index.html
  3. +41 −72 examples/chat/server.js
View
@@ -19,26 +19,26 @@ $(document).ready(function () {
// client receives a message
minitaur.on("message", function(data) {
var chatArea = $("#area-chat");
-
+
switch(data.cmd) {
case "in":
chatArea.append(
"<div class=\"msg\">" +
- data.sid +
+ data.cid +
" connected!</div>"
);
break;
case "out":
chatArea.append(
"<div class=\"msg\">" +
- data.sid +
+ data.cid +
" disconnected!</div>"
);
break;
case "msg":
chatArea.append(
"<div class=\"msg\"><b>" +
- data.sid +
+ data.cid +
"</b>: " + data.content + "</div>"
);
break;
@@ -47,21 +47,31 @@ $(document).ready(function () {
}
chatArea.scrollTop(chatArea[0].scrollHeight);
});
-
+
// client disconnects from server
minitaur.on("disconnect", function() {
var chatArea = $("#area-chat");
chatArea.append(
- "<div class=\"msg\">Disconnected, reconnecting ...</div>"
+ "<div class=\"msg\">Disconnected ...</div>"
+ );
+ chatArea.scrollTop(chatArea[0].scrollHeight);
+ });
+
+ // client error handler
+ minitaur.on("error", function(data) {
+ var chatArea = $("#area-chat");
+ chatArea.append(
+ "<div class=\"msg\">Error, type: " + data.type +
+ ", message: " + data.message + "</div>"
);
chatArea.scrollTop(chatArea[0].scrollHeight);
});
-
+
// initiate client connection with server
minitaur.connect({
- host: "localhost:8080"
+ host: "my.domain.com:8080"
});
-
+
// bind button click event for sending message
$("#button-send").click(function() {
sendMessage();
View
@@ -1,33 +1,33 @@
<!DOCTYPE html>
<html>
- <head>
- <title>proto-crossdomain</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="robots" content="noindex,nofollow" />
- </head>
- <body>
- <div id="container">
- <div id="area-chat">
- </div>
- <div id="area-input">
- <input type="text" autocomplete="off" id="text-send" />
- <input type="submit" value="Send" id="button-send" />
- </div>
- <div id="debug">
- </div>
- </div>
-
- <style>
- i { background: red; }
- body { background: white; font-family: Verdana, Arial; font-size: 12px; margin: 20px 0 0 0; padding: 0; }
- #container { margin: 0px auto; position: relative; width: 990px; }
- #area-chat { border-top: 1px solid black; border-bottom: 1px solid black; height: 250px; margin: 16px 0 16px 0; overflow: auto; padding: 8px 0 8px 0; width: 100%; }
- #area-input { width: 100%; }
- .msg { padding: 4px 0; width: 100%; }
- </style>
-
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
- <script src="minitaur.js" type="text/javascript"></script>
- <script src="client.js" type="text/javascript"></script>
- </body>
-</html>
+<head>
+ <title>Minotaur chat example</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <meta name="robots" content="noindex,nofollow" />
+</head>
+<body>
+ <div id="container">
+ <div id="area-chat">
+ </div>
+ <div id="area-input">
+ <input type="text" autocomplete="off" id="text-send" />
+ <input type="submit" value="Send" id="button-send" />
+ </div>
+ <div id="debug">
+ </div>
+ </div>
+
+ <style>
+ i { background: red; }
+ body { background: white; font-family: Verdana, Arial; font-size: 12px; margin: 20px 0 0 0; padding: 0; }
+ #container { margin: 0px auto; position: relative; width: 990px; }
+ #area-chat { border-top: 1px solid black; border-bottom: 1px solid black; height: 250px; margin: 16px 0 16px 0; overflow: auto; padding: 8px 0 8px 0; width: 100%; }
+ #area-input { width: 100%; }
+ .msg { padding: 4px 0; width: 100%; }
+ </style>
+
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js" type="text/javascript"></script>
+ <script src="minitaur.js" type="text/javascript"></script>
+ <script src="client.js" type="text/javascript"></script>
+</body>
+</html>
View
@@ -1,91 +1,60 @@
-var util = require("util"),
- http = require("http"),
- url = require("url"),
- qs = require("querystring"),
- fs = require("fs"),
-Minotaur = require("../../lib/server/minotaur"),
- PORT = 8080;
+var http = require("http");
+ util = require("util"),
+ fs = require("fs"),
+ Minotaur = require("../../lib/server/Minotaur");
-// http server for serving static files
-var httpServer = http.createServer(function(req, res) {
- var path = url.parse(req.url).pathname;
- switch(path) {
+var httpServer = http.createServer(function(req, res) {
+ switch(req.url) {
case "/":
- fs.readFile("./index.html", function (err, data) {
- res.writeHead(200, {"Content-Type": "text/html"});
- res.write(data, "utf8");
- res.end();
- });
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.write(fs.readFileSync(__dirname + '/index.html'));
+ res.end();
break;
- case "/client.js":
- fs.readFile("./" + path, function(err, data){
- res.writeHead(200, {"Content-Type": "text/javascript"});
- res.write(data, "utf8");
- res.end();
- });
+ case "/favicon.ico":
+ res.writeHead(200, {'Content-Type': 'image/x-icon'} )
+ res.end();
+ break;
+ case "/minitaur.js":
+ res.writeHead(200, {'Content-Type': 'text/javascript'});
+ res.write(fs.readFileSync('../../lib/client/minitaur.js'));
+ res.end();
+ break;
+ case "/client.js":
+ res.writeHead(200, {'Content-Type': 'text/javascript'});
+ res.write(fs.readFileSync(__dirname + '/client.js'));
+ res.end();
break;
- case "/minitaur.js":
- fs.readFile("../../lib/client/" + path, function(err, data){
- res.writeHead(200, {"Content-Type": "text/javascript"});
- res.write(data, "utf8");
- res.end();
- });
- break;
default:
break;
}
});
-httpServer.listen(PORT);
-util.log("Listening on port " + PORT);
+httpServer.listen(8080);
+util.log("Running on 8080");
// set up minotaur with settings
var minotaur = new Minotaur({
server: httpServer,
- pollTimeout: 15000,
- disconnectTimeout: 20000
+ subdomainPool: ["www1", "www2"]
});
-// client connects to server
-minotaur.on("connect", function(session) {
- // console message about new session connection
- util.log("+S " + session.sid + " [" + minotaur.sessionsCount + "]");
-
- // broadcast message to everyone (except this client connection) that this
- // client has connected
- minotaur.broadcast({cmd: "in", sid: session.sid}, session.sid);
-
- // new session client has connected
- session.on("clientConnect", function(clientID) {
- // console message about new session client connection
- util.log(" +C " + clientID + " [" + session.clientsCount + "]");
- });
-
- // session client has disconnected
- session.on("clientDisconnect", function(clientID) {
- // console message about session client disconnection
- util.log(" -C " + clientID + " [" + session.clientsCount + "]");
- });
-
- // client receives message
- session.on("message", function(message) {
- if(message && message.cmd && message.content) {
- // broadcast message to everyone
- minotaur.broadcast({
- cmd: message.cmd,
- sid: session.sid,
- content: message.content
- });
- }
+// client connects to server
+minotaur.on("connect", function(client) {
+ util.log("+C " + client.id + " [" + minotaur.clientsCount + ", " + minotaur.sessionsCount + "]");
+ minotaur.broadcast({cmd: "in", cid: client.id}, client.id);
+
+ client.on("message", function(data) {
+ util.log("msg " + client.id);
+
+ minotaur.broadcast({
+ cmd: data.cmd,
+ cid: client.id,
+ content: data.content
+ });
});
- // client disconnected from server
- session.on("disconnect", function() {
- // console message about session disconnection
- util.log("-S " + session.sid + " [" + minotaur.sessionsCount + "]");
-
- // broadcast to all connected sessions message about this session
- // disconnection
- minotaur.broadcast({cmd: "out", sid: session.sid});
+ client.on("disconnect", function(reason) {
+ util.log("-C " + client.id + " [" + minotaur.clientsCount + ", " + minotaur.sessionsCount + "] reason: " + reason);
+ minotaur.broadcast({cmd: "out", cid: client.id});
});
});

0 comments on commit 14dcd3f

Please sign in to comment.