Created
May 13, 2012 18:05
-
-
Save grantmichaels/2689537 to your computer and use it in GitHub Desktop.
NodeJS HTTP Chat Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"); | |
var sys = require("sys"); | |
var io = require("socket.io"); | |
var ChatServer = {}; | |
ChatServer.members = {}; | |
ChatServer.publish = function(client, data) { | |
if (client in ChatServer.members) | |
for (var sessionId in ChatServer.members) | |
ChatServer.members[sessionId].send(client.sessionId + ": " + data); | |
else sys.puts("[ERROR] Unsubscribed client message: " + client); | |
}; | |
ChatServer.subscribe = function(client) { | |
if (client.sessionId in ChatServer.members) | |
sys.puts("[ERROR] Client has already subscribed: " + client); | |
else ChatServer.members[client.sessionId] = client; | |
}; | |
ChatServer.handle = function(client, message) { | |
switch (message.action) { | |
case "subscribe": | |
ChatServer.subscribe(client); | |
break; | |
case "send": | |
ChatServer.send(client, message.data); | |
break; | |
default: | |
sys.puts("[ERROR] Invalid message: " + message); | |
} | |
}; | |
ChatServer.render = function(req, res) { | |
res.render("index.ejs", {layout: false}); | |
}; | |
var app = express.createServer(); | |
app.use("/static", express.static(__dirname + "/static")); | |
app.get("/", ChatServer.render); | |
var socket = io.listen(app); | |
socket.on("connection", function(client) { | |
client.on("message", function(message) { | |
ChatServer.handle(client, message); | |
}); | |
}); | |
app.listen(3000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var socket = io.connect("http://localhost"); | |
// Set socket listeners. | |
socket.on("connect", function() { | |
$("#board").append("<p>Connected!</p>"); | |
}); | |
socket.on("message", function(data) { | |
$("#board").append("<p>" + data + "</p>"); | |
}); | |
socket.on("disconnect", function() { | |
$("#board").append("<p>Disconnected!</p>"); | |
}); | |
// Subscribe to the channel. | |
socket.send({action: "subscribe"}); | |
// Set send event. | |
$("#data").keypress(function(ev) { | |
if (ev.which == 13) | |
socket.send({action: "publish", data: $("#data").val()}, | |
function() { $("#data").val(""); }); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>ChatServer</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<script type="text/javascript" src="/socket.io/socket.io.js"></script> | |
<script type="text/javascript" src="/static/jquery.js"></script> | |
<script type="text/javascript" src="/static/client.js"></script> | |
</head> | |
<body> | |
<div id="board"></div> | |
<input id="data" type="text" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment