Skip to content

Instantly share code, notes, and snippets.

@lamorbidamacchina
Last active November 22, 2021 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamorbidamacchina/8eb6c1f9228fb848760726ccb4681af0 to your computer and use it in GitHub Desktop.
Save lamorbidamacchina/8eb6c1f9228fb848760726ccb4681af0 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
app.use(express.static('public'));
var http = require('http').Server(app);
var port = process.env.PORT || 3001;
// setup my socket server
var io = require('socket.io')(http);
io.on('connection', function(socket) {
console.log('New connection');
const count = io.engine.clientsCount;
console.log("Connected clients: " + count);
io.emit('participants',count);
// This is called when the client calls socket.emit('message')
socket.on('message', function(obj) {
console.log('Chatlog - ' + obj.userid + ': ' + obj.msg);
// socket.broadcast.emit('message', msg); // to all, but the sender
io.emit('message',obj); // to all, including the sender
});
// This is called when a client disconnects
socket.on('disconnect', function() {
console.log('Disconnection');
const count = io.engine.clientsCount;
console.log("Connected clients: " + count);
io.emit('participants',count);
});
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/default.html');
});
http.listen(port, function() {
console.log('listening on *: ' + port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment