Skip to content

Commit

Permalink
merged binary with upstream's new chat app
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-roark committed Feb 19, 2014
2 parents beab053 + 69de792 commit 8c3ca99
Show file tree
Hide file tree
Showing 8 changed files with 546 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ language: node_js
node_js:
- "0.8"
- "0.10"
- "0.11"

matrix:
fast_finish: true
allow_failures:
- node_js: "0.11"

notifications:
irc: "irc.freenode.org#socket.io"
19 changes: 19 additions & 0 deletions examples/chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Socket.IO Chat
==============

A simple chat demo for socket.io

### Quickstart
```
$ npm install
$ node app
```

### Features
- Multiple users can join a chat room by entering a unique username on website load.
- Users can type chat messages to the chat room
- A notification is sent to all users when a user joins or leaves the chatroom

### Uses
- Express
- jQuery
77 changes: 77 additions & 0 deletions examples/chat/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('../..').listen(server);
var port = process.env.PORT || 8000;

server.listen(port, function () {
console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(__dirname + '/public'));

// Chatroom

// usernames which are currently connected to the chat
var usernames = {};
var numUsers = 0;

io.on('connection', function (socket) {
var addedUser = false;

// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});

// when the client emits 'add user', this listens and executes
socket.on('add user', function (username) {
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username
});
});

// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});

// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});

// when the user disconnects.. perform this
socket.on('disconnect', function () {
// remove the username from global usernames list
if (addedUser) {
delete usernames[socket.username];
--numUsers;

// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username
});
}
});
});
12 changes: 12 additions & 0 deletions examples/chat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "socket.io-chat",
"version": "0.0.0",
"description": "A simple chat client using socket.io",
"main": "app.js",
"author": "Grant Timmerman",
"private": true,
"license": "BSD",
"dependencies": {
"express": "3.4.8"
}
}
28 changes: 28 additions & 0 deletions examples/chat/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Socket.IO Chat Example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<ul class="pages">
<li class="chat page">
<div class="chatArea">
<ul class="messages"></ul>
</div>
<input class="inputMessage" placeholder="Type here..."/>
</li>
<li class="login page">
<div class="form">
<h3 class="title">What's your nickname?</h3>
<input class="usernameInput" type="text" maxlength="14" />
</div>
</li>
</ul>

<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/main.js"></script>
</body>
</html>
Loading

0 comments on commit 8c3ca99

Please sign in to comment.