-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged binary with upstream's new chat app
- Loading branch information
Showing
8 changed files
with
546 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
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 |
This file contains 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
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 | ||
}); | ||
} | ||
}); | ||
}); |
This file contains 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
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" | ||
} | ||
} |
This file contains 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
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> |
Oops, something went wrong.