-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·175 lines (148 loc) · 4.59 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// A Painter application that uses MQTT to distribute draw events
// to all other devices running this app.
/* global Paho device */
var app = {}
var host = 'mqtt.evothings.com'
var port = 1884
app.connected = false
app.ready = false
app.uuid = getUUID()
app.nickName = "Anonymous"
function getUUID () {
if (window.isCordovaApp) {
var uuid = device.uuid
if ((uuid.length) > 16) {
// On iOS we get a uuid that is too long, strip it down to 16
uuid = uuid.substring(uuid.length - 16, uuid.length)
}
return uuid
} else {
return guid()
}
}
/**
* Generates a GUID string.
* @returns {String} The generated GUID.
* @example af8a8416-6e18-a307-bd9c-f2c947bbb3aa
* @author Slavik Meltser ([email protected]).
* @link http://slavik.meltser.info/?p=142
*/
function guid () {
function _p8 (s) {
var p = (Math.random().toString(16) + '000000000').substr(2, 8)
return s ? '-' + p.substr(0, 4) + '-' + p.substr(4, 4) : p
}
return _p8() + _p8(true) + _p8(true) + _p8()
}
// Simple function to generate a color from the device UUID
app.generateColor = function (uuid) {
var code = parseInt(uuid.split('-')[0], 16)
var blue = (code >> 16) & 31
var green = (code >> 21) & 31
var red = (code >> 27) & 31
return 'rgb(' + (red << 3) + ',' + (green << 3) + ',' + (blue << 3) + ')'
}
app.initialize = function () {
console.log("init")
document.addEventListener(
'deviceready',
app.onReady,
false)
}
app.onReady = function () {
if (!app.ready) {
app.color = app.generateColor(app.uuid) // Generate our own color from UUID
app.pubTopic = 'batman/' + app.uuid + '/evt' // We publish to our own device topic
app.subTopic = 'batman/+/evt' // We subscribe to all devices using "+" wildcard
app.setupChat()
app.setupConnection()
app.setNickname()
app.ready = true
}
}
app.setupChat = function () {
var chatbox = document.getElementById("chatbox")
app.chatbox = chatbox
var sendmessage = document.getElementById('sendmessage')
sendmessage.addEventListener('click', function (event) {
if (app.connected) {
var usermessage = document.getElementById('usermessage')
var msg = JSON.stringify({
name: app.nickName, message: usermessage.value, color: app.color})
app.publish(msg)
document.getElementById('usermessage').value = '';
}
})
}
app.setupConnection = function () {
app.status('Connecting to ' + host + ':' + port + ' as ' + app.uuid)
app.client = new Paho.MQTT.Client(host, port, app.uuid)
app.client.onConnectionLost = app.onConnectionLost
app.client.onMessageArrived = app.onMessageArrived
var options = {
useSSL: true,
onSuccess: app.onConnect,
onFailure: app.onConnectFailure
}
app.client.connect(options)
}
app.publish = function (json) {
var message = new Paho.MQTT.Message(json)
message.destinationName = app.pubTopic
app.client.send(message)
console.log("Published message: " + message)
}
app.subscribe = function () {
app.client.subscribe(app.subTopic)
console.log('Subscribed: ' + app.subTopic)
}
app.unsubscribe = function () {
app.client.unsubscribe(app.subTopic)
console.log('Unsubscribed: ' + app.subTopic)
}
app.onMessageArrived = function (message) {
// here put message in new div as child
console.log("Received json: " + message)
console.log(app.color + "app color")
var o = JSON.parse(message.payloadString)
if(o.message.includes('@' + app.nickName)) {
var message = document.createElement("div")
message.innerHTML = o.name + ": " + o.message
message.style.color = o.color;
app.chatbox.appendChild(message)
console.log("Appended: " + message.innerHTML)
} else if(!o.message.includes('@')) {
var message = document.createElement("div")
message.innerHTML = o.name + ": " + o.message
message.style.color = o.color;
app.chatbox.appendChild(message)
console.log("Appended: " + message.innerHTML)
}
}
app.onConnect = function (context) {
app.subscribe()
app.status('Connected!')
app.connected = true
}
app.onConnectFailure = function (e) {
console.log('Failed to connect: ' + JSON.stringify(e))
}
app.onConnectionLost = function (responseObject) {
app.status('Connection lost!')
console.log('Connection lost: ' + responseObject.errorMessage)
app.connected = false
}
app.status = function (s) {
console.log(s)
var info = document.getElementById('info')
info.innerHTML = s
}
app.setNickname = function () {
var nickName = document.getElementById("nickName")
nickName.addEventListener('change', function (event) {
if (app.connected) {
app.nickName = nickName.value
}
})
}
app.initialize()