Skip to content

Commit 54bdbf4

Browse files
committed
first commit
0 parents  commit 54bdbf4

File tree

16 files changed

+339
-0
lines changed

16 files changed

+339
-0
lines changed

Diff for: .gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
node_modules
3+
dump.rdb
4+
npm-debug.log
5+
tmp
6+
client/static/assets

Diff for: .nodemonignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Ignore file for Nodemon: https://github.com/remy/nodemon
2+
# Install with 'npm install -g nodemon' then start your app with 'nodemon app.js'
3+
# From then on, all changes you make to /server will cause your app to automatically restart
4+
5+
/client/*
6+
./README.md
7+
.git

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Welcome to your new realtime app

Diff for: app.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// My SocketStream 0.3 app
2+
3+
var http = require('http'),
4+
ss = require('socketstream');
5+
6+
// Define a single-page client called 'main'
7+
ss.client.define('main', {
8+
view: 'app.html',
9+
css: ['libs/reset.css', 'app.css'],
10+
code: ['libs/jquery.min.js', 'app'],
11+
tmpl: '*'
12+
});
13+
14+
// Serve this client on the root URL
15+
ss.http.route('/', function(req, res){
16+
res.serveClient('main');
17+
});
18+
19+
// Use server-side compiled Hogan (Mustache) templates. Others engines available
20+
ss.client.templateEngine.use(require('ss-hogan'));
21+
22+
// Minimize and pack assets if you type: SS_ENV=production node app.js
23+
if (ss.env === 'production') ss.client.packAssets();
24+
25+
// Start web server
26+
var server = http.Server(ss.http.middleware);
27+
server.listen(3000);
28+
29+
// Start SocketStream
30+
ss.start(server);

Diff for: client/code/app/app.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* QUICK CHAT DEMO */
2+
3+
// Delete this file once you've seen how the demo works
4+
5+
// Listen out for newMessage events coming from the server
6+
ss.event.on('newMessage', function(message) {
7+
8+
// Example of using the Hogan Template in client/templates/chat/message.jade to generate HTML for each message
9+
var html = ss.tmpl['chat-message'].render({
10+
message: message,
11+
time: function() { return timestamp(); }
12+
});
13+
14+
// Append it to the #chatlog div and show effect
15+
return $(html).hide().appendTo('#chatlog').slideDown();
16+
});
17+
18+
// Show the chat form and bind to the submit action
19+
$('#demo').on('submit', function() {
20+
21+
// Grab the message from the text box
22+
var text = $('#myMessage').val();
23+
24+
// Call the 'send' funtion (below) to ensure it's valid before sending to the server
25+
return exports.send(text, function(success) {
26+
if (success) {
27+
return $('#myMessage').val('');
28+
} else {
29+
return alert('Oops! Unable to send message');
30+
}
31+
});
32+
});
33+
34+
// Demonstrates sharing code between modules by exporting function
35+
exports.send = function(text, cb) {
36+
if (valid(text)) {
37+
return ss.rpc('demo.sendMessage', text, cb);
38+
} else {
39+
return cb(false);
40+
}
41+
};
42+
43+
44+
// Private functions
45+
46+
var timestamp = function() {
47+
var d = new Date();
48+
return d.getHours() + ':' + pad2(d.getMinutes()) + ':' + pad2(d.getSeconds());
49+
};
50+
51+
var pad2 = function(number) {
52+
return (number < 10 ? '0' : '') + number;
53+
};
54+
55+
var valid = function(text) {
56+
return text && text.length > 0;
57+
};

Diff for: client/code/app/entry.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This file automatically gets called first by SocketStream and must always exist
2+
3+
// Make 'ss' available to all modules and the browser console
4+
window.ss = require('socketstream');
5+
6+
ss.server.on('disconnect', function(){
7+
console.log('Connection down :-(');
8+
});
9+
10+
ss.server.on('reconnect', function(){
11+
console.log('Connection back up :-)');
12+
});
13+
14+
ss.server.on('ready', function(){
15+
16+
// Wait for the DOM to finish loading
17+
jQuery(function(){
18+
19+
// Load app
20+
require('/app');
21+
22+
});
23+
24+
});

Diff for: client/code/libs/jquery.min.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: client/css/app.css

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* Example CSS file */
2+
3+
/* Main */
4+
5+
body,
6+
html {
7+
height: 100%;
8+
}
9+
body {
10+
font: normal 1em sans-serif;
11+
background: linear-gradient(top, #eeeeee, #ffffff);
12+
text-align: center;
13+
}
14+
p {
15+
margin-bottom: 1em;
16+
}
17+
a {
18+
color: #000a68;
19+
}
20+
h1 {
21+
font-size: 1.5em;
22+
font-weight: normal;
23+
margin: 1.5em;
24+
color: #333;
25+
text-shadow: 1px 1px 2px white;
26+
}
27+
#content {
28+
padding: 50px;
29+
}
30+
31+
/* Quick Chat Demo */
32+
33+
#demo {
34+
border: 1px solid #ccc;
35+
width: 700px;
36+
text-align: left;
37+
margin: 50px auto;
38+
padding: 15px;
39+
border-radius: 5px;
40+
}
41+
#demo h3 {
42+
font-size: 1.0em;
43+
font-weight: normal;
44+
}
45+
#demo h5 {
46+
padding: 5px 0;
47+
color: #666;
48+
font-weight: normal;
49+
font-size: 0.8em;
50+
}
51+
#demo #myMessage,
52+
#demo #chatlog {
53+
border: none;
54+
box-shadow: inset 0 0 2px #777;
55+
border-radius: 5px;
56+
}
57+
#demo #myMessage {
58+
width: 690px;
59+
padding: 5px;
60+
}
61+
#demo #chatlog {
62+
width: 700px;
63+
background-color: white;
64+
margin: 5px 0;
65+
}
66+
#demo #chatlog p {
67+
font-size: 0.9em;
68+
color: #000a68;
69+
padding: 3px 9px;
70+
margin: 0;
71+
}
72+
#demo #chatlog p:first-child {
73+
padding-top: 9px;
74+
}
75+
#demo #chatlog p:last-child {
76+
padding-bottom: 9px;
77+
}
78+
#demo #chatlog span.time {
79+
color: #666;
80+
float: right;
81+
width: 70px;
82+
text-align: right;
83+
font-size: 0.7em;
84+
}

Diff for: client/css/libs/reset.css

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* http://meyerweb.com/eric/tools/css/reset/
2+
v2.0 | 20110126
3+
License: none (public domain)
4+
*/
5+
6+
html, body, div, span, applet, object, iframe,
7+
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
8+
a, abbr, acronym, address, big, cite, code,
9+
del, dfn, em, img, ins, kbd, q, s, samp,
10+
small, strike, strong, sub, sup, tt, var,
11+
b, u, i, center,
12+
dl, dt, dd, ol, ul, li,
13+
fieldset, form, label, legend,
14+
table, caption, tbody, tfoot, thead, tr, th, td,
15+
article, aside, canvas, details, embed,
16+
figure, figcaption, footer, header, hgroup,
17+
menu, nav, output, ruby, section, summary,
18+
time, mark, audio, video {
19+
margin: 0;
20+
padding: 0;
21+
border: 0;
22+
font-size: 100%;
23+
font: inherit;
24+
vertical-align: baseline;
25+
}
26+
/* HTML5 display-role reset for older browsers */
27+
article, aside, details, figcaption, figure,
28+
footer, header, hgroup, menu, nav, section {
29+
display: block;
30+
}
31+
body {
32+
line-height: 1;
33+
}
34+
ol, ul {
35+
list-style: none;
36+
}
37+
blockquote, q {
38+
quotes: none;
39+
}
40+
blockquote:before, blockquote:after,
41+
q:before, q:after {
42+
content: '';
43+
content: none;
44+
}
45+
table {
46+
border-collapse: collapse;
47+
border-spacing: 0;
48+
}

Diff for: client/static/favicon.ico

1.12 KB
Binary file not shown.

Diff for: client/static/images/logo.png

28.5 KB
Loading

Diff for: client/templates/chat/message.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>
2+
<span class="time">{{time}}</span>
3+
<span class="message">{{message}}</span>
4+
</p>

Diff for: client/views/app.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<SocketStream/>
6+
<title>Welcome</title>
7+
</head>
8+
<body>
9+
<div id="content">
10+
<a href="https://github.com/socketstream/socketstream">
11+
<img src="/images/logo.png" alt="SocketStream Logo" width="160" height="160">
12+
</a>
13+
<h1>Welcome to your new realtime app!</h1>
14+
15+
<!-- QUICK CHAT DEMO-->
16+
<form id="demo" onsubmit="return false">
17+
<h3>Quick Chat Demo</h3>
18+
<h5>Open this page in multiple tabs or browsers and type a message below</h5>
19+
<div id="chatlog"></div>
20+
<input id="myMessage" type="text" autocomplete="off">
21+
</form>
22+
</div>
23+
</body>
24+
</html>

Diff for: package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "test-429",
3+
"description": "An awesome real time application",
4+
"version": "0.0.1",
5+
"author": "Me <[email protected]>",
6+
"private": true,
7+
"engines": { "node": ">= 0.6.0" },
8+
"dependencies": {
9+
"socketstream": "0.3.x",
10+
"ss-hogan": "0.1.x"
11+
},
12+
"scripts":{
13+
"start":"node app.js"
14+
}
15+
}

Diff for: server/middleware/example.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Example request middleware
2+
3+
// Only let a request through if the session has been authenticated
4+
exports.authenticated = function() {
5+
return function(req, res, next) {
6+
if (req.session && (req.session.userId != null)) {
7+
return next();
8+
} else {
9+
return res(false);
10+
}
11+
};
12+
};

Diff for: server/rpc/demo.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Server-side Code
2+
3+
// Define actions which can be called from the client using ss.rpc('demo.ACTIONNAME', param1, param2...)
4+
exports.actions = function(req, res, ss) {
5+
6+
// Example of pre-loading sessions into req.session using internal middleware
7+
req.use('session');
8+
9+
// Uncomment line below to use the middleware defined in server/middleware/example
10+
//req.use('example.authenticated')
11+
12+
return {
13+
14+
sendMessage: function(message) {
15+
if (message && message.length > 0) { // Check for blank messages
16+
ss.publish.all('newMessage', message); // Broadcast the message to everyone
17+
return res(true); // Confirm it was sent to the originating client
18+
} else {
19+
return res(false);
20+
}
21+
}
22+
23+
};
24+
25+
};

0 commit comments

Comments
 (0)