forked from kaazing/javascript.getting.started
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (59 loc) · 2.95 KB
/
index.js
File metadata and controls
68 lines (59 loc) · 2.95 KB
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
'use strict';
/* index.js - this is the client AngularJS code for the AngularJS starter app
* This shows the basic process for establishing a WebSocket connection and sending/receiving messages
* It makes use of jQuery (only to manipulate the DOM) as well as the Kaazing Javascript UniversalClient library
*/
angular.module("webSocketApp", ['ngSanitize'])
.controller("mainCtl", function ($scope, $log, $timeout) {
var connectionInfo = {
url: "wss://sandbox.kaazing.net/amqp091", // URL to Kaazing's public sandbox for WebSocket testing
username: "guest",
password: "guest"
},
topicPub = "websocket-starter", // the 'publication' topic in pub-sub
topicSub = "websocket-starter"; // the matching 'subscription' topic in pub-sub
$scope.clientID = "Client" + Math.random().toString(36).substring(2, 15); // generate a random ID
$scope.messageCounter = 1;
$scope.message = "";
$scope.messageToSend = null;
$scope.client = UniversalClientDef("amqp");
$scope.subscription = {};
$scope.onMessage = function (msg) {
$log.info("Received server message: " + msg.message);
var text = $scope.message.trim();
if (text.length) {
text += "<br/>";
}
// Need a small timeout for the angular binding to work
$timeout(function () {
$scope.message = text + msg.message;
}, 100);
};
$scope.onError = function (err) { // replace this with your own error handler
alert(err);
};
$scope.client.connect(connectionInfo,
$scope.onError, // callback to process errors
function (conn) {
conn.subscribe(
topicPub, // Topic to send messages
topicSub, // Topic to subscribe to receive messsages
$scope.onMessage, // callback function to process received message
false, // noLocal flag - setting this to 'false' allows you to receive your own messages
function (sub) {
$scope.subscription = sub;
console.info("Subscription is created " + $scope.subscription);
$scope.subscription.sendMessage({message: "From " + $scope.clientID + ": Initial message is sent!"});
$scope.messageToSend = "Message " + $scope.messageCounter + " sent!";
});
}
);
$(window).unload(function () {
$scope.client.close();
});
$scope.sendMessageOnClick = function () {
$scope.subscription.sendMessage({message: "From " + $scope.clientID + ": " + $scope.messageToSend});
$scope.messageCounter++;
$scope.messageToSend = "Message " + $scope.messageCounter + " is sent!";
};
});