2
這可能是一個(也可能是)愚蠢的問題,但我一直在試圖建立一個服務器,將與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>
是否可以從網頁的按鈕,一個變量交回給主服務器,並有它不斷更新?
啊謝謝你,我不知道這件事。 – Bort