-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (47 loc) · 1.53 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
(function() {
"use strict";
var Skeletor = this.Skeletor || {};
this.Skeletor.Mobile = this.Skeletor.Mobile || new Skeletor.App();
var app = this.Skeletor.Mobile;
app.config = null;
app.requiredConfig = {
wakeful: {
url: 'string'
}
};
app.client = null;
app.init = function() {
// create Faye client pointing at Wakeful server
app.client = new Faye.Client('http://coati.encorelab.org:7890/faye');
app.setupEventHandler();
// subscribe to a channel to receive messages
app.subscribeToChannel('/foo');
},
app.setupEventHandler = function () {
jQuery( "#messageForm" ).submit(function( event ) {
var message = JSON.stringify({text: jQuery('#messageText').val()});
// console.log(message);
var publication = app.client.publish('/foo', message);
publication.then(function() {
console.log('Message received by server!');
// clear the input field
jQuery('#messageText').val('');
}, function(error) {
alert('There was a problem: ' + error.message);
});
event.preventDefault();
});
},
app.subscribeToChannel = function (streamName) {
var subscription = app.client.subscribe(streamName, function(jsonMessage) {
var message = JSON.parse(jsonMessage);
// handle message
console.log(message);
var msgList = jQuery('#messages');
var msgItem = jQuery('<li>'+message.text+'</li>');
msgList.append(msgItem);
});
}
// this.Skeletor.Mobile = app;
this.Skeletor = Skeletor;
}).call(this);