Node JS Socket.IO

//—————- HTTPS Server —————————————-
var fs = require(‚fs‘);
var https = require(‚https‘);
var express = require(‚express‘);
var app = express();

var options = {
key: fs.readFileSync(‚/etc/letsencrypt/live/hg-system.com/privkey.pem‘),
cert: fs.readFileSync(‚/etc/letsencrypt/live/hg-system.com/fullchain.pem‘)
};

var server = https.createServer(options, app);
var port = 3000;

server.listen(port, function() {
log(‚listening on *:‘ + port);
});

app.use(express.static(‚public‘));

app.get(‚/‘, (req, res) => {
res.sendFile(‚index.html‘);
});

//—————- Socket.IO Extention —————————————-
var io = require(’socket.io‘)(server);

/*

//—————- cases of io emits

socket.broadcast.to(too.room).emit(‚message‘, msg); // sending to all clients in ‚game‘ room(channel) except sender
socket.to(too.room).emit(‚message‘, msg); // sending to sender client, only if they are in ‚game‘ room(channel)
socket.emit(‚video_message‘, msg); break; // sending to sender-client only
socket.broadcast.emit(‚video_message‘, msg); break; // sending to all clients except sender
io.emit(‚video_message‘, msg); break; // sending to all clients include sender
socket.broadcast.to(too).emit(‚message‘, msg); // sending to individual socketid

*/

//—————- IO —————————————-
io.on(‚connection‘, function(socket) {

socket.on(„*“, function(data){
console.log(`${ clients[socket.id].nick } (<<) „${data}“`); });   socket.on(‚disconnect‘, () => {
log(Color.cyan, `info ${ clients[socket.id].nick } from ${ clients[socket.id].conn.handshake.address } disconnected.`);
//send2.too = too.others;
emit(‚clients‘, „client “ + clients[socket.id].nick, send2.others ); //to all others
delete clients[socket.id];
});

socket.on(‚join‘, function(room) {
console.log(`${ clients[socket.id].nick } joining room ${room}`);
socket.join(room);
});

socket.on(‚leave‘, function(room) {
console.log(`${ clients[socket.id].nick } leaving room ${room}`);
socket.leave(room);
});

socket.on(„register“, (data) => {
clients[socket.id].nick = data.nick;
clients[socket.id].pass = data.pass;
});

});

Schreibe einen Kommentar