Skip to content
Browse files

make the example semi-interesting

  • Loading branch information...
1 parent f4b66a3 commit 76a7973299a433dd7ffb42f9d45c3f12577e8593 @lloyd committed Apr 6, 2012
Showing with 35 additions and 6 deletions.
  1. +21 −3 index.html
  2. +14 −3 server.js
View
24 index.html
@@ -1,8 +1,26 @@
+<!DOCTYPE html>
+<html>
+<head><title>socket.io example on awsbox</title></head>
+<body>
+ Type a message<br/>
+ <input/> <button> send it </button>
+ <br/><br/>
+ See them messages
+ <pre></pre>
+</body>
<script src="/socket.io/socket.io.js"></script>
+<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
- var socket = io.connect('http://localhost');
- socket.on('news', function (data) {
+ var socket = io.connect('http://localhost:3000');
+ socket.on('chat', function (data) {
+ $("pre").text($("pre").text() + "\n" + data);
console.log(data);
- socket.emit('my other event', { my: 'data' });
+ });
+
+ $("button").click(function() {
+ var msg = $.trim($("input").val());
+ console.log("sending", msg);
+ socket.emit("chat", msg);
});
</script>
+</html>
View
17 server.js
@@ -7,9 +7,20 @@ app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
+var clients = [];
+
+function broadcast(msg) {
+ clients.forEach(function(socket) {
+ socket.emit('chat', msg);
+ });
+}
+
io.sockets.on('connection', function (socket) {
- socket.emit('news', { hello: 'world' });
- socket.on('my other event', function (data) {
- console.log(data);
+ clients.push(socket);
+ socket.on('chat', function (data) {
+ broadcast(data);
+ });
+ socket.on('disconnect', function (data) {
+ clients.splice(clients.indexOf(socket), 1);
});
});

0 comments on commit 76a7973

Please sign in to comment.
Something went wrong with that request. Please try again.