I'm working to create a simple chat demo that will send messages to flash
clients over a socket connection. I also need to be able to tell node to
send a message to all connected clients. Currently, I'm hitting the node
http server with a browser to test this.
I have the socket chat working, but when I hit the node http server, the
clients do not receive messages. I'm using 'broadcast()' to send messages
in both cases. I can see that the broadcast() function is being called in
both cases. Why is the "Hello from HTTP server" message not received by
clients?
// chat.js
net = require('net');
var clients = [];
function broadcast(message) {
console.log('message: ' + message);
clients.forEach(function (client) {
client.write(message);
});
}
// socket chat server
net.createServer(function (socket) {
socket.name = socket.remoteAddress + ":" + socket.remotePort
clients.push(socket);
broadcast(socket.name + " joined chat ");
socket.on('data', function (data) {
console.log('on data: ' + data);
broadcast(socket.name + "> " + data);
});
}).listen(9000);
console.log('started socket server:at 10.0.0.60:9000');
// http server
var http = require('http');
http.createServer(function (req, res) {
broadcast("Hello from HTTP server");
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(9500);
console.log('Server running at http://10.0.0.60:9500/');
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en