-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (53 loc) · 2.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// guida ufficiale socket.io: https://socket.io/get-started/chat
const express = require('express');
const app = express();
const http = require('http');
// viene inizializzata app in modo che possa fornire un server HTTP
const server = http.createServer(app);
const { Server } = require("socket.io");
// creo una nuova istanza di socket.io passando l'oggetto HTTP drrv
const io = new Server(server);
app.get('/',(req,res)=>{
// con res.sendFile e "servire" index.html invece di scrivere tutto l'HTML tra le parentesi
res.sendFile(__dirname + '/socketChat.html');
});
// rimango in ascolto sull'evento connection per un socket in entrata e loggo sulla console
// per la disconnessione ??
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('a user disconnected');
});
});
// Broadcasting metodo io.emit()
// questo emetterà l'evento a tutti gli utenti connessi
io.emit('some event', { someProperty: 'some value', otherProperty: 'other value' });
// chat message event
// per inviare il messaggio a tutti tranne a chi l'ha emesso bisogna modificare questo evento
// applicando il flag 'broadcast'
io.on('connection', (socket) =>{
socket.on('chat message', (msg) =>{
io.emit('chat message', msg);
});
});
io.sockets.on('connection', function(socket){
socket.on('create',function(room){
socket.join(room);
});
});
server.listen(3000, ()=>{
console.log('listening on *:3000');
});
//funzia tutto, guida finisce così
// studiare miglioroie
// socket.io è composto da:
// un server che monta NOdeJs Http server
// una libreria client che emula il browser side
// durante lo sviluppo socket.io "serve" il client automaticamente
//https://stackoverflow.com/questions/62196729/socket-io-client-server-unicast-channels-how-to-implement-them
// questo tipo su stackoverflow che ha pensato
// le chat one-to-one come stanze(rooms) con un id = nome_utente
// in modo che i messaggi in broadcast sono inviati solo all'utente connesso e al mittente
// così ogni utente ha la sua stanza
// secondo questo sistema l'helper dalla dashboard cliccando sull'utente "libero",
// può creare una chat con solo l'utente dentro