-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (78 loc) · 2.41 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//require('newrelic');
var express = require('express'),
http = require('http'),
path = require('path'),
hogan = require('hogan-express'),
util = require('util'),
app = express(),
config = require('./config'),
server = require('http').createServer(app),
azure = require('azure'),
io = require('socket.io').listen(server);
var serviceBusService = azure.createServiceBusService(config.sbConnectionString);
var currentSockets = [];
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.engine('mustache', hogan);
app.set('view engine', 'mustache');
app.set('layout', __dirname + '/views/layout');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function() {
app.use(express.errorHandler());
});
app.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
// Handle 'connection' events
io.sockets.on('connection', function(socket) {
socket.emit('fromServer', {
message: 'Connected! There are now ' + io.sockets.clients().length + ' clients connected.'
});
currentSockets.push(socket);
//setInterval(function() { sendSampleMessage(socket) }, 5000);
socket.on('message', function(data) {
socket.emit('message', {
message: 'I sent: ' + data.message
});
socket.broadcast.emit('message', {
message: data.message
});
});
});
server.listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});
function sendSampleMessage(socket) {
var msg = sample.getSample();
socket.emit('fromServer', {
message: msg
});
}
function getMessage() {
if(currentSockets.length > 0) {
serviceBusService.receiveSubscriptionMessage(config.sbTopic, config.sbSubscription, function(error, receivedMessage) {
if(!error) {
for(var i = currentSockets.length - 1; i >= 0; i--) {
console.log("[" + i + "] writing " + receivedMessage.body );
var copy = new Buffer(receivedMessage.body);
currentSockets[i].emit('fromServer', {
message: copy
});
};
} else {
console.log("Error recieving message");
console.log(error);
}
getMessage();
});
} else {
setTimeout(getMessage, 3000);
}
}
getMessage();