这可能是一个(也可能是)愚蠢的问题,但我一直在尝试设置一个可以使用http和websockets的服务器。我希望按下按钮的结果可以更改服务器中的值,然后可以通过websocket发送。
这是我的主要服务器代码:
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
app.use(function (req, res, next) {
console.log('middleware');
req.testing = 'device-protocol';
return next();
});
app.get('/button.htm', function (req, res) {
res.sendFile( __dirname + "/" + "button.htm" );
});
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log("Message received from client: %s", msg);
ws.send(JSON.stringify(msg));
given, should query
});
console.log('socket', req.testing);
});
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log("Test app listening at http://%s:%s", host, port)
})
这是我的按钮网页:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8 /angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.showMe = false;
$scope.myFunc = function() {
$scope.showMe = !$scope.showMe;
}
});
</script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="myFunc()">Click Me!</button>
</div>
</body>
</html>
是否可以将网页按钮中的变量传回主服务器并让其不断更新?