fixing the websocket http led client example

This commit is contained in:
simon 2019-04-28 23:30:44 +02:00
parent 1cf70b19a9
commit f31e1a6330

View File

@ -1,35 +1,38 @@
<html> <html>
<head>
</head> <head>
<body ng-app="stripApp" ng-controller="stripCtrl" >
</head>
<body ng-app="stripApp" ng-controller="stripCtrl">
{{data}} {{data}}
<div class="col-12" ng-repeat="led in data" style="border-style: solid;display: table"> <div class="col-12" ng-repeat="led in data" style="border-style: solid;display: table">
<div ng-repeat="(key, value) in led" style="width: 60px; height: 60px; background: {{key}}; display: table-cell;"> <div ng-repeat="(key, value) in led"
style="width: 60px; height: 60px; background: {{key}}; display: table-cell;">
{{value}} {{value}}
</div> </div>
</div> </div>
<script src="node_modules/angular/angular.min.js"></script> <script src="node_modules/angular/angular.min.js"></script>
<script src="node_modules/reconnecting-websocket/dist/reconnecting-websocket-iife.min.js"></script> <script src="node_modules/reconnecting-websocket/dist/reconnecting-websocket-iife.min.js"></script>
<script> <script>
var app = angular.module('stripApp',[]); var app = angular.module('stripApp', []);
app.factory('socket', [function() { app.factory('socket', [function () {
var stack = []; var stack = [];
var onmessageDefer; var onmessageDefer;
var host = "localhost" var host = "localhost"
if (window.location.hostname != ""){ if (window.location.hostname != "") {
host = window.location.hostname host = window.location.hostname
} }
var socket = { var socket = {
ws: new ReconnectingWebSocket("ws://"+host+":8001", null, {debug: false, reconnectInterval: 2000}), ws: new ReconnectingWebSocket("ws://" + host + ":8001", null, { debug: false, reconnectInterval: 2000 }),
send: function(data) { send: function (data) {
if (socket.ws.readyState == 1) { if (socket.ws.readyState == 1) {
socket.ws.send(data); socket.ws.send(data);
} else { } else {
stack.push(data); stack.push(data);
} }
}, },
onmessage: function(callback) { onmessage: function (callback) {
if (socket.ws.readyState == 1) { if (socket.ws.readyState == 1) {
socket.ws.onmessage = callback; socket.ws.onmessage = callback;
} else { } else {
@ -37,7 +40,7 @@
} }
} }
}; };
socket.ws.onopen = function(event) { socket.ws.onopen = function (event) {
for (i in stack) { for (i in stack) {
socket.ws.send(stack[i]); socket.ws.send(stack[i]);
} }
@ -50,18 +53,18 @@
return socket; return socket;
}]); }]);
app.controller('stripCtrl', ['$scope', 'socket', function($scope, socket) { app.controller('stripCtrl', ['$scope', 'socket', function ($scope, socket) {
$scope.connected = false; $scope.connected = false;
$scope.data = {} $scope.data = {}
socket.ws.addEventListener('close',function(event) { socket.ws.addEventListener('close', function (event) {
$scope.$apply(function(){ $scope.$apply(function () {
$scope.connected = false $scope.connected = false
}); });
}); });
socket.ws.addEventListener('open',function(event) { socket.ws.addEventListener('open', function (event) {
$scope.$apply(function(){ $scope.$apply(function () {
socket.send( socket.send(
JSON.stringify({ JSON.stringify({
'register_client_type': 1, 'register_client_type': 1,
@ -72,8 +75,8 @@
}); });
}); });
socket.onmessage(function(event) { socket.onmessage(function (event) {
$scope.$apply(function(){ $scope.$apply(function () {
console.log(event.data) console.log(event.data)
json = JSON.parse(event.data) json = JSON.parse(event.data)
$scope.data = json $scope.data = json
@ -81,5 +84,6 @@
}); });
}]); }]);
</script> </script>
</body> </body>
</html> </html>