我有这个非常简单的应用程序:
// Require HTTP module (to start server) and Socket.IO
var http = require('http'),
io = require('socket.io'),
url = require('url');
var server = http.createServer(function(req, res){
req.on('data', function(data){
console.log('Received Data');
})
});
server.listen(3000);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// Add a connect listener
socket.on('connection', function(client){
server.on('data',function(event){
client.send(url.search);
console.log('Received Data from socket app');
});
client.on('disconnect',function(){
console.log('Server has disconnected');
});
});
我需要卷曲节点服务器以传递一些帖子数据,然后我希望将这些数据传递给socket.io应用程序,以便它可以反馈给当前连接的客户端。
无论如何要做到这一点?
非常感谢,
答案 0 :(得分:2)
您从
获得的socket
var socket = io.listen(server);
您可以随时拨打emit
向所有已连接的客户端发送消息。因此,在您的http服务器中,您可以emit
发布的数据:
var server = http.createServer(function(req, res){
var body = '';
req.on('data', function(data){
console.log('Received Data');
body += data;
});
req.on('end', function() {
// Emit the data to all clients
socket.emit('foo message', body);
});
});