使用Node.js和socket.io,以下工作正常,試圖將廣播回客戶端。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);
這是因爲'broadcast'是'io.sockets.socket'對象不是的'io.sockets'對象(一個方法(這將是'你的情況client')比如你錯誤地命名的套接字)。你想'client.broadcast ...'。 – ebohlman