Skip to content

Commit bb9f9f3

Browse files
committed
Added Node Chatroom Demo 9
1 parent 2f391ff commit bb9f9f3

24 files changed

+9326
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
<Name>09_NodeChatroom</Name>
7+
<RootNamespace>09_NodeChatroom</RootNamespace>
8+
</PropertyGroup>
9+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
10+
<PropertyGroup>
11+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
12+
<SchemaVersion>2.0</SchemaVersion>
13+
<ProjectGuid>216fef5b-bae6-44db-8ebd-16e6d09842f7</ProjectGuid>
14+
<ProjectHome>.</ProjectHome>
15+
<StartupFile>
16+
</StartupFile>
17+
<StartWebBrowser>False</StartWebBrowser>
18+
<SearchPath>
19+
</SearchPath>
20+
<WorkingDirectory>.</WorkingDirectory>
21+
<OutputPath>.</OutputPath>
22+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
23+
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
24+
<ProjectView>ShowAllFiles</ProjectView>
25+
<StartWebBrowser>false</StartWebBrowser>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
28+
<DebugSymbols>true</DebugSymbols>
29+
</PropertyGroup>
30+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
31+
<DebugSymbols>true</DebugSymbols>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Folder Include="public\" />
35+
<Folder Include="public\css\" />
36+
<Folder Include="public\fonts\" />
37+
<Folder Include="public\js\" />
38+
<Folder Include="routes\" />
39+
<Folder Include="views\" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Content Include="package.json" />
43+
<Content Include="public\css\bootstrap-theme.css" />
44+
<Content Include="public\css\bootstrap-theme.css.map" />
45+
<Content Include="public\css\bootstrap-theme.min.css" />
46+
<Content Include="public\css\bootstrap.css" />
47+
<Content Include="public\css\bootstrap.css.map" />
48+
<Content Include="public\css\bootstrap.min.css" />
49+
<Content Include="public\css\style.styl" />
50+
<Content Include="public\fonts\glyphicons-halflings-regular.eot" />
51+
<Content Include="public\fonts\glyphicons-halflings-regular.svg" />
52+
<Content Include="public\fonts\glyphicons-halflings-regular.ttf" />
53+
<Content Include="public\fonts\glyphicons-halflings-regular.woff" />
54+
<Content Include="views\index.jade" />
55+
<Content Include="views\layout.jade" />
56+
<Content Include="Web.config" />
57+
<Content Include="Web.Debug.config" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<Compile Include="app.js" />
61+
<Compile Include="public\js\bootstrap.js" />
62+
<Compile Include="public\js\bootstrap.min.js" />
63+
<Compile Include="public\js\index.js" />
64+
<Compile Include="routes\index.js" />
65+
<Compile Include="routes\user.js" />
66+
</ItemGroup>
67+
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
68+
</Project>

09_NodeChatroom/Web.Debug.config

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- This file will modify Web.config to enable remote debugging when publishing your project in the Debug configuration.
3+
For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
4+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
5+
6+
<system.web xdt:Transform="Insert">
7+
<httpRuntime targetFramework="4.5" />
8+
<customErrors mode="Off"/>
9+
</system.web>
10+
11+
<system.webServer>
12+
13+
<iisnode loggingEnabled="true"
14+
devErrorsEnabled="true"
15+
interceptor="--debug"
16+
xdt:Transform="SetAttributes" />
17+
18+
<handlers>
19+
<!-- The GUID in the following path is meant to protect the debugging endpoint against inadvertent access, and should be treated as a password. -->
20+
<add name="NtvsDebugProxy" path="ntvs-debug-proxy/1e8eca2b-61ff-4d44-8d2a-45821bb48eaf" verb="*" resourceType="Unspecified"
21+
type="Microsoft.NodejsTools.Debugger.WebSocketProxy, Microsoft.NodejsTools.WebRole"
22+
xdt:Transform="Insert" />
23+
</handlers>
24+
25+
<rewrite>
26+
<rules>
27+
<rule name="NtvsDebugProxy" enabled="true" stopProcessing="true" xdt:Transform="InsertAfter(/configuration/system.webServer/rewrite/rules/clear)">
28+
<match url="^ntvs-debug-proxy/.*"/>
29+
</rule>
30+
</rules>
31+
</rewrite>
32+
33+
</system.webServer>
34+
35+
</configuration>

09_NodeChatroom/Web.config

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
For more information on how to configure your Node.js application, please visit
4+
http://go.microsoft.com/fwlink/?LinkId=290972
5+
-->
6+
<configuration>
7+
<appSettings>
8+
<!--
9+
<add key="StorageAccountName" value="" />
10+
<add key="StorageAccountKey" value="" />
11+
<add key="ServiceBusNamespace" value="" />
12+
<add key="ServiceBusIssuerName" value="" />
13+
<add key="ServiceBusIssuerSecretKey" value="" />
14+
-->
15+
</appSettings>
16+
<system.webServer>
17+
<webSocket enabled="false" />
18+
19+
<modules runAllManagedModulesForAllRequests="false" />
20+
21+
<!-- Web.Debug.config adds attributes to this to enable remote debugging when publishing in Debug configuration. -->
22+
<iisnode watchedFiles="web.config;*.js"/>
23+
24+
<!-- indicates that the server.js file is a node.js application
25+
to be handled by the iisnode module -->
26+
<handlers>
27+
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
28+
</handlers>
29+
<rewrite>
30+
<rules>
31+
<clear />
32+
<rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
33+
<match url="iisnode.+" negate="true" />
34+
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
35+
<action type="Rewrite" url="app.js" />
36+
</rule>
37+
</rules>
38+
</rewrite>
39+
</system.webServer>
40+
</configuration>

09_NodeChatroom/app.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+

2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var express = require('express');
7+
var routes = require('./routes');
8+
var user = require('./routes/user');
9+
var http = require('http');
10+
var path = require('path');
11+
12+
var mongo = require('mongodb').MongoClient;
13+
14+
var app = express();
15+
16+
// all environments
17+
app.set('port', process.env.PORT || 3000);
18+
app.set('views', path.join(__dirname, 'views'));
19+
app.set('view engine', 'jade');
20+
app.use(express.favicon());
21+
app.use(express.logger('dev'));
22+
app.use(express.json());
23+
app.use(express.urlencoded());
24+
app.use(express.methodOverride());
25+
app.use(app.router);
26+
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
27+
app.use(express.static(path.join(__dirname, 'public')));
28+
29+
// development only
30+
if ('development' == app.get('env')) {
31+
app.use(express.errorHandler());
32+
}
33+
34+
app.get('/', routes.index);
35+
app.get('/users', user.list);
36+
37+
var serve = http.createServer(app);
38+
var io = require('socket.io')(serve);
39+
40+
serve.listen(app.get('port'), function () {
41+
console.log('Express server listening on port ' + app.get('port'));
42+
});
43+
44+
io.on('connection', function (socket) {
45+
console.log('a user connected');
46+
47+
mongo.connect(process.env.CUSTOMCONNSTR_MONGOLAB_URI, function (err, db) {
48+
if(err){
49+
console.warn(err.message);
50+
} else {
51+
var collection = db.collection('chat messages')
52+
var stream = collection.find().sort().limit(10).stream();
53+
stream.on('data', function (chat) { console.log('emitting chat'); socket.emit('chat', chat.content); });
54+
}
55+
});
56+
57+
socket.on('disconnect', function () {
58+
console.log('user disconnected');
59+
});
60+
61+
socket.on('chat', function (msg) {
62+
mongo.connect(process.env.CUSTOMCONNSTR_MONGOLAB_URI, function (err, db) {
63+
if(err){
64+
console.warn(err.message);
65+
} else {
66+
var collection = db.collection('chat messages');
67+
collection.insert({ content: msg }, function (err, o) {
68+
if (err) { console.warn(err.message); }
69+
else { console.log("chat message inserted into db: " + msg); }
70+
});
71+
}
72+
});
73+
74+
socket.broadcast.emit('chat', msg);
75+
});
76+
});

09_NodeChatroom/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "_09_NodeChatroom",
3+
"version": "0.0.0",
4+
"description": "09_NodeChatroom",
5+
"main": "app.js",
6+
"author": {
7+
"name": "Rami Sayar",
8+
"email": ""
9+
},
10+
"dependencies": {
11+
"express": "^3.4.4",
12+
"jade": "*",
13+
"mongodb": "^1.4.19",
14+
"socket.io": "^1.0.6",
15+
"stylus": "^0.49.1"
16+
}
17+
}

0 commit comments

Comments
 (0)