Node.js 从服务器端调用客户端函数
从服务器端调用客户端函数时遇到问题。我目前正在服务器端使用文件监视程序。我希望发生的是,当文件被更改时,它将触发客户端函数 来自服务器端的代码 来自客户端的代码Node.js 从服务器端调用客户端函数,node.js,Node.js,从服务器端调用客户端函数时遇到问题。我目前正在服务器端使用文件监视程序。我希望发生的是,当文件被更改时,它将触发客户端函数 来自服务器端的代码 来自客户端的代码 实际代码看起来不像是从后端调用callThisFunction,查找它应该会有所帮助 您应该做的是在前端创建一个套接字和该套接字的侦听器。一旦启动该套接字,您就可以在客户端中运行任何您想要的内容 坏习惯: 不使用套接字,您可以每秒向服务器发送一个请求(setInterval(sendRequest,1000)),该请求检查后端是否有更新
实际代码看起来不像是从后端调用
callThisFunction
,查找它应该会有所帮助
您应该做的是在前端创建一个套接字和该套接字的侦听器。一旦启动该套接字,您就可以在客户端中运行任何您想要的内容
坏习惯:
不使用套接字,您可以每秒向服务器发送一个请求(setInterval(sendRequest,1000)
),该请求检查后端是否有更新,您可以使用类似于{response:{data:{fireThisFunction:true}}
的响应。
我之所以提供这种解决方案,是因为它速度更快,而且您可能会发现甚至不需要从后端启动函数。您可以使用WebSocket将更改从服务器传递到客户端,您可以使用这种方法在客户端上有效地调用函数 在本地运行服务器,然后在浏览器中打开index.html server.js
const WebSocketServer = require("websocket").server;
const http = require("http");
const filewatcher = require("filewatcher");
const server = http.createServer((request, response) => {
});
server.listen(8080, function() {
console.log("Server is listening on port 8080");
});
webSocketServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
let webConn = null;
webSocketServer.on("request", function(request) {
console.log("New Connection...");
const connection = request.accept("some-protocol", request.origin);
webConn = connection;
connection.on("message", function(message) {
if (message.type === "utf8") {
console.log("New Message from client:" + message.utf8Data);
}
});
});
// Start watching directory..
const watcher = filewatcher();
watcher.add("./watch_dir");
watcher.on("change", function(folder, stat) {
console.log("file change:", folder, stat);
if (webConn) {
webConn.sendUTF(JSON.stringify({ folder, stat}));
}
});
index.html
<html lang="en">
<meta charset="utf-8">
<head>
<title>Websocket demo</title>
<script>
console.log("Sending hello..")
const webSocket = new WebSocket("ws://localhost:8080/", "some-protocol");
webSocket.onopen = function (event) {
webSocket.send("Ping from client..");
};
webSocket.onmessage = function (event) {
console.log("Message from server:", event.data);
callThisFunction(event.data);
}
function callThisFunction(data) {
document.getElementById("output").innerHTML += "<br><li>Message from server:" + (data) + "</li>";
}
</script>
</head>
<body>
<div>
<ul id="output">
</ul>
</div>
</body>
</html>
Websocket演示
console.log(“发送hello…”)
const webSocket=newwebsocket(“ws://localhost:8080/”,“一些协议”);
webSocket.onopen=函数(事件){
发送(“从客户端Ping…”);
};
webSocket.onmessage=函数(事件){
日志(“来自服务器的消息:”,event.data);
调用此函数(event.data);
}
函数调用此函数(数据){
document.getElementById(“输出”).innerHTML+=“
来自服务器的消息:”+(数据)+“ ”;
}
您可能必须socket.io
从服务器端发出事件,在客户端监听并执行您的功能
const WebSocketServer = require("websocket").server;
const http = require("http");
const filewatcher = require("filewatcher");
const server = http.createServer((request, response) => {
});
server.listen(8080, function() {
console.log("Server is listening on port 8080");
});
webSocketServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
let webConn = null;
webSocketServer.on("request", function(request) {
console.log("New Connection...");
const connection = request.accept("some-protocol", request.origin);
webConn = connection;
connection.on("message", function(message) {
if (message.type === "utf8") {
console.log("New Message from client:" + message.utf8Data);
}
});
});
// Start watching directory..
const watcher = filewatcher();
watcher.add("./watch_dir");
watcher.on("change", function(folder, stat) {
console.log("file change:", folder, stat);
if (webConn) {
webConn.sendUTF(JSON.stringify({ folder, stat}));
}
});
<html lang="en">
<meta charset="utf-8">
<head>
<title>Websocket demo</title>
<script>
console.log("Sending hello..")
const webSocket = new WebSocket("ws://localhost:8080/", "some-protocol");
webSocket.onopen = function (event) {
webSocket.send("Ping from client..");
};
webSocket.onmessage = function (event) {
console.log("Message from server:", event.data);
callThisFunction(event.data);
}
function callThisFunction(data) {
document.getElementById("output").innerHTML += "<br><li>Message from server:" + (data) + "</li>";
}
</script>
</head>
<body>
<div>
<ul id="output">
</ul>
</div>
</body>
</html>