所以我有这个想法,当你运行nodejs脚本时,它启动端口8080上的http(快速)服务器和端口1337上的常规tcp(网络)服务器。当你通过netcat连接到tcp服务器并发送“警告“它通过javascript在网页上运行alert()命令。这是代码但if语句不起作用(它打印数据但没有在我的Web浏览器上发出警报)
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var net = require("net");
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
var server = net.createServer();
io.on("connection", function(sock) {
console.log("Client connected");
server.on("connection", function(conn) {
var remoteAddress = conn.remoteAddress + ":" + conn.remotePort;
console.log("New client: " + remoteAddress);
conn.on("data", function (data) {
console.log(data.toString())
if (data.toString() == "alert") {
console.log("YES ALERT");
io.emit("HELLO");
}
});
conn.once("close", function () {
console.log(remoteAddress + " disconnected");
});
conn.on("error", function (error) {
console.log(remoteAddress + " Error: " + error);
});});
});
server.listen(1337, function() {
console.log("Listening on 1337");
});
http.listen(8080, function() {
console.log("Web server started on 8080");
});
答案 0 :(得分:0)
在您的网页中包含socket io客户端js并连接到套接字服务器
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:1337');
socket.on('connect', function(){});
socket.on('event', function(data){ alert(data.message) });
socket.on('disconnect', function(){});
</script>
您可以更改服务器端代码,发出事件以使用上面的示例。
io.emit("event",{"message":"HELLO"});