-1
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
app.use('/js',express.static(__dirname+'/js'));
app.get('/', function(req, res){
res.sendfile('codeschool_chattr.html');
});
io.on('connection', function(client){
console.log('a user connected');
var nick="";
client.on('join',function(name){
client.nickname=name;
});
client.on('disconnect', function(){
console.log('user disconnected');
});
client.on('chat message', function(msg){
client.broadcast.emit('chat message', msg);
});
});
我有一個套接字io應用程序代碼,這裏的聊天消息正在發送到除發送它的人之外的所有連接的客戶端,我應該怎麼做,或者我錯過了什麼使之發生。 也在這裏是我的前端腳本 -爲什麼socketio廣播發送給所有的客戶,但發送它的人?
<script src="/socket.io/socket.io.js"></script>
<script>
var server = io.connect('http://localhost:3000');
server.on('connect',function(data){
var nick = prompt(" What do your friends call you? ");
server.emit('join',nick);
});
$('form').submit(function(){
server.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
server.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
在發佈一個新的之前嘗試搜索舊問題:http://stackoverflow.com/questions/7352164/update-all-clients-using-socket-io/7352443#7352443 – user568109