//引用
var io = require('socket.io')(server);
//server
io.on('connection', function(socket) {
//推送消息
socket.emit('news', 'hello!!'); //進來後 給連接的人推送消息
io.sockets.emit('news','hello!!'); //進來後 給全部人推送消息
//接收消息
socket.on('message', function(data) {
console.log(data);
socket.emit('news', data);
socket.broadcast.emit('news',data); //向全部在線人員其餘人發消息
});
});
//cli
var iosocket = io.connect();
//發送
$('#mesBtn').click(function(event) {
var text = $('#nickInput').val();
iosocket.emit('message', text);
console.log(iosocket);
});
//接收
iosocket.on('news', function (data) {
console.log(data);
});
可參考一下:http://www.cnblogs.com/edwardstudy/p/4358202.html