【发布时间】:2016-11-28 11:12:49
【问题描述】:
我的目标是将我的 html 文件触发的文本 left 或 right 发送到连接到服务器的所有客户端(我的 nodejs 服务器文件)。我尝试了许多在线解决方案,但我无法理解,其中一些无法正常工作,因为我不是普通的 nodejs 程序员。
我在 html 中创建了带有两个按钮 left 和 right 的 Web 应用程序。当我单击 html 文件上的左按钮时,我所有连接的客户端都应该得到文本“左”。我还创建了server.js nodejs 文件,它可以与所有客户端完美通信。问题是如何在 html 文件和服务器文件之间进行通信。
注意: html 文件是服务器端触发点而不是 client。
server.js
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 8888;
net.createServer(function (sock) {
console.log('connected : ' + sock.remoteAddress + ':'+sock.remotePort);
var ip = sock.remoteAddress.split(':')[3];
console.log(ip);
sock.write('hello client you are connected with server 10.0.0.19...');
sock.on('data', function (data) {
console.log('DATA '+ sock.remoteAddress+' - '+data);
sock.write('you said : '+data);
});
function sendto(result) { //tried call from html file
console.log('sent '+result);
sock.write('data : '+result);
}
sock.on('close', function (data) {
console.log('closed');
});
}).listen(PORT);
console.log('server running on '+PORT);
帮助我将我的html file 与server.js 交流,但不是client。
【问题讨论】:
标签: javascript html node.js