You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Vue-Socket.io/example/server/server.js

65 lines
1.0 KiB
JavaScript

9 years ago
var socket = require('socket.io');
var io = socket.listen(1923);
var users = [];
var messages = [];
var names = [];
io.on('connection', function(client){
9 years ago
client.on('join', function(name){
9 years ago
client.join('chat');
users.push(name);
names[client.id] = name;
client.emit('users', users);
client.emit('messages', messages);
client.emit('joined', true);
client.broadcast.emit('adduser', name);
});
client.on('send', function(message){
9 years ago
var data = {
name: names[client.id],
message: message
};
if(messages.length > 10){
9 years ago
messages.splice(0, 10);
}
messages.push(data);
io.emit('onmessage', data);
});
client.on('disconnect', function(){
9 years ago
var name = names[client.id];
delete names[client.id];
var index = users.indexOf(name);
if(index!=-1){
9 years ago
delete users[index];
}
users = users.filter(Boolean)
9 years ago
io.emit('users', users);
});
});