Node.js 如何使用socket.write将app.post事件(从express)写入到客户端?
当其中一个客户机在输入字段(文本框)中发布消息时,我试图以html形式从输入字段向连接到服务器的所有客户机发送消息。我想使用socket.write函数,因为我有一个原始TCP客户端连接到服务器。 我尝试在Post事件中使用Express库,因为我想要这种行为,但不能在Node.js 如何使用socket.write将app.post事件(从express)写入到客户端?,node.js,express,Node.js,Express,当其中一个客户机在输入字段(文本框)中发布消息时,我试图以html形式从输入字段向连接到服务器的所有客户机发送消息。我想使用socket.write函数,因为我有一个原始TCP客户端连接到服务器。 我尝试在Post事件中使用Express库,因为我想要这种行为,但不能在app.Post函数中使用socket,例如socket.write函数 以下是我目前在服务器端的内容: var express = require('express'); var bodyParser = require('bo
app.Post
函数中使用socket,例如socket.write
函数
以下是我目前在服务器端的内容:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var net = require('net');
var server = net.createServer(function (socket) {
socket.write(' + req.body.name + ');
socket.pipe(socket);
});
app.get('/', function(req, res){
res.sendfile('index.html');
});
app.post('/myaction', function(req, res) {
res.send('You sent the name "' + req.body.name + '".');
//How to do this here??
server.socket.write(' + req.body.name + ')
socket.end();
});
app.listen(3000, function() {
console.log('Server running at http://127.0.0.1:3000/');
});
//Thought of using a different server port for the raw TCP device
server.listen(2000, '127.0.0.1');
以及客户端的html表单:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Form</title>
</head>
<body>
<div id="contact">
<h1>Send an email</h1>
<form action="http://127.0.0.1:3000/myaction" method="post">
<fieldset>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Your name" />
<label for="email">Surname:</label>
<input type="text" id="surname" placeholder="Your surname" />
<input type="submit" value="Send" />
</fieldset>
</form>
</div>
</body>
</html>
形式
发送电子邮件
姓名:
姓:
是否有方法在app.post事件
上执行socket.write
。并且可以使用socket.write
发送输入字段中的名称
抱歉,如果我遗漏了什么,我是这种编程的新手。您需要跟踪您的TCP客户端。试试这个:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var net = require('net');
// All your clients
var clients = [];
var server = net.createServer(function (socket) {
// Track the client
clients.push(socket);
socket.on('data', function (data) {
// Handle data coming from clients here
});
});
app.get('/', function(req, res){
res.sendfile('index.html');
});
app.post('/myaction', function(req, res) {
res.send('You sent the name "' + req.body.name + '".');
broadcast('New name:' + req.body.name);
});
// Here's where you send data to all clients
function broadcast(msg) {
clients.forEach(function (client) {
client.write(msg);
});
}
app.listen(3000, function() {
console.log('Server running at http://127.0.0.1:3000/');
});
//Thought of using a different server port for the raw TCP device
server.listen(2000, '127.0.0.1');
您需要跟踪您的TCP客户端。试试这个:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var net = require('net');
// All your clients
var clients = [];
var server = net.createServer(function (socket) {
// Track the client
clients.push(socket);
socket.on('data', function (data) {
// Handle data coming from clients here
});
});
app.get('/', function(req, res){
res.sendfile('index.html');
});
app.post('/myaction', function(req, res) {
res.send('You sent the name "' + req.body.name + '".');
broadcast('New name:' + req.body.name);
});
// Here's where you send data to all clients
function broadcast(msg) {
clients.forEach(function (client) {
client.write(msg);
});
}
app.listen(3000, function() {
console.log('Server running at http://127.0.0.1:3000/');
});
//Thought of using a different server port for the raw TCP device
server.listen(2000, '127.0.0.1');
非常感谢你。好东西。我很高兴,如果它解决了你的问题,请记为正确答案。谢谢。好东西。我很乐意,如果它解决了你的问题,就把它标记为正确答案