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