使用 Node.js 和 socket.io,以下工作正常,直到尝试广播回客户端。
我的广播未定义?
var http = require('http'),
sys = require('sys'),
fs = require('fs'),
io = require('socket.io');
var server = http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
var rs = fs.createReadStream(__dirname + '/index.html');
sys.pump(rs, response);
});
var socket = io.listen(server);
debugger;
socket.on('connection', function(client) {
debugger;
var username;
client.send('Welcome to this socket.io chat server!');
client.send('Please input your username: ');
client.on('message', function(message) {
if (!username) {
username = message;
client.emit('Welcome, ' + username + '!');
return;
}
socket.broadcast.send('a message');
//io.socket.send('a message');
//io.socket.emit('message', username + ' sent: ' + message);
});
});
server.listen(4000);